﻿#create_appropriate_relic_artifact_effect
#create_artifact_pedestal_bible_effect
#create_artifact_pedestal_thurible_effect
#create_artifact_pedestal_situla_effect
#create_artifact_pedestal_crozier_effect
#create_christian_religious_artifact_effect
#pam_miles_christi_white_peace_effect
#pam_miles_christi_fulfillment_from_combat_effect
#pam_miles_christi_holy_weapon_victory_effect
#pam_miles_christi_holy_weapon_gain_victory_effect
#pam_miles_christi_holy_weapon_blessing_effect
#pam_tenet_scholasticism_university_bonus_effect
#pam_tenet_scholasticism_yearly_effect
#pam_tenet_scholasticism_studium_effect
#pam_harmonious_society_five_bonds_effect
#pam_harmonious_society_apply_five_bonds_effect
#pam_tenet_purgatory_sin_penalty_effect
#pam_tenet_apostolic_succession_episcopate_effect
#pam_tenet_divine_liturgy_choirs_effect
#pam_tenet_divine_liturgy_officiant_effect
#pam_pentarchy_circuit_entourage_effect
#pam_pentarchy_pilgrim_circuit_effect
#add_scholar_trait_or_xp_effect
#pam_remove_forbidden_personal_tenet_secret_if_invalid_effect
#pam_faith_cleanup_forbidden_personal_tenet_secrets_effect
#pam_faith_grant_forbidden_personal_tenet_secrets_effect
#on_personal_tenet_gain_secret_gain_effect
#pam_ai_personal_tenet_pulse_effect
#conversion_tenet_acts_of_the_apostles_legitimacy_effect
#ora_et_labora_gardener_gain_effect
#hedonistic_reveler_gain_effect
#hedonistic_obesity_modifier_sync_effect
#hedonistic_feast_gout_risk_effect
#pam_tenet_mountain_worship_rough_terrain_expert_gain_effect
#pam_tenet_mountain_worship_hike_spiritual_fulfillment_effect
#asceticism_inappetetic_gain_effect
#literalism_scholar_gain_effect
#scholasticism_scholar_gain_effect
#pam_levy_the_tithes_decision_effects
#unrelenting_faith_blademaster_gain_effect
#pam_kingdom_of_heaven_declaration_effect
#pam_create_theocratic_dynasty_extra_effects
#pam_the_christian_church_clergy_embezzled_treasury_catalyst_effect
#pam_the_christian_church_non_christian_kingdom_in_europe_catalyst_effect
#pam_the_christian_church_county_converted_to_faith_catalyst_effect
#pam_the_christian_church_county_conversion_catalysts_effect
#pam_the_christian_church_excommunication_war_catalyst_effect
#pam_the_christian_church_humiliation_war_catalyst_effect
#pam_the_christian_church_clergy_has_child_catalyst_effect
#pam_determine_ghw_target_character_from_kingdom_effect
#pam_use_bull_selected_ghw_target_effect
#pam_start_bull_great_holy_war_effect
#pam_bull_notify_faith_rulers_effect
#pam_declare_rite_heresy_effect
#pam_secure_pilgrim_routes_effect
#request_white_peace_interaction_effect
#request_victory_interaction_effect
#request_enforce_succession_interaction_effect
#pam_enforce_title_succession_effect
#pam_increase_papabile_score_effect
#pam_on_conversion_effect
#pam_cardinal_vacancy_on_death_effect
#pam_evangelize_personal_tenet_effect
#pam_evangelize_personal_tenet_decline_effect

create_appropriate_relic_artifact_effect = {
	hidden_effect_new_object = {
		if = {
			limit = { religion = religion:christianity_religion }
			create_artifact_pedestal_reliquary_christian_effect = { OWNER = $OWNER$ }
		}
		else_if = {
			limit = { religion = religion:islam_religion }
			create_artifact_pedestal_reliquary_islam_effect = { OWNER = $OWNER$ }
		}
		else_if = {
			limit = { religion = religion:buddhism_religion }
			create_artifact_pedestal_reliquary_buddhism_effect = { OWNER = $OWNER$ }
		}
		else_if = {
			limit = { religion = religion:judaism_religion }
			create_artifact_pedestal_reliquary_judaism_effect = { OWNER = $OWNER$ }
		}
		else = {
			create_artifact_sculpture_religious_effect = { OWNER = $OWNER$ CREATOR = $CREATOR$ }
		}
		scope:newly_created_artifact = {
			set_variable = relic # Adds the Relic flag
		}
		if = { # Makes a regular character artifact if you don't have a Royal Court
			limit = {
				has_royal_court = no
			}
			scope:newly_created_artifact = {
				reforge_artifact = { type = miscellaneous }
			}
		}
	}
}

create_artifact_pedestal_bible_effect = {
	$OWNER$ = { save_scope_as = owner }
	$CREATOR$ = { save_scope_as = creator }

	get_artifact_quality_effect = yes
	get_artifact_wealth_effect = yes

	scope:owner = {
		create_artifact = {
			name = artifact_pedestal_bible_name
			creator = scope:creator
			description = artifact_pedestal_bible_description
			visuals = pam_bible
			type = pedestal
			template = cross_template
			modifier = artifact_placeholder_modifier
			save_scope_as = newly_created_artifact
			wealth = scope:wealth
			quality = scope:quality
		}
	}

	scope:newly_created_artifact = {
		add_scaled_artifact_modifier_devotion_effect = yes
		add_scaled_artifact_modifier_grandeur_small_effect = yes
		if = {
			limit = {
				OR = {
					rarity = famed
					rarity = illustrious
				}
			}
			add_scaled_artifact_modifier_piety_effect = yes
		}
		remove_artifact_modifier = artifact_placeholder_modifier
	}
}

create_artifact_pedestal_thurible_effect = {
	$OWNER$ = { save_scope_as = owner }
	$CREATOR$ = { save_scope_as = creator }

	get_artifact_quality_effect = yes
	get_artifact_wealth_effect = yes

	scope:owner = {
		create_artifact = {
			name = artifact_pedestal_thurible_name
			creator = scope:creator
			description = artifact_pedestal_thurible_description
			visuals = pam_thurible
			type = pedestal
			template = cross_template
			modifier = artifact_placeholder_modifier
			save_scope_as = newly_created_artifact
			wealth = scope:wealth
			quality = scope:quality
		}
	}

	scope:newly_created_artifact = {
		add_scaled_artifact_modifier_devotion_effect = yes
		add_scaled_artifact_modifier_grandeur_small_effect = yes
		if = {
			limit = {
				OR = {
					rarity = famed
					rarity = illustrious
				}
			}
			add_scaled_artifact_modifier_piety_effect = yes
		}
		remove_artifact_modifier = artifact_placeholder_modifier
	}
}

create_artifact_pedestal_situla_effect = {
	$OWNER$ = { save_scope_as = owner }
	$CREATOR$ = { save_scope_as = creator }

	get_artifact_quality_effect = yes
	get_artifact_wealth_effect = yes

	scope:owner = {
		create_artifact = {
			name = artifact_pedestal_situla_name
			creator = scope:creator
			description = artifact_pedestal_situla_description
			visuals = pam_situla
			type = pedestal
			template = cross_template
			modifier = artifact_placeholder_modifier
			save_scope_as = newly_created_artifact
			wealth = scope:wealth
			quality = scope:quality
		}
	}

	scope:newly_created_artifact = {
		add_scaled_artifact_modifier_devotion_effect = yes
		add_scaled_artifact_modifier_grandeur_small_effect = yes
		if = {
			limit = {
				OR = {
					rarity = famed
					rarity = illustrious
				}
			}
			add_scaled_artifact_modifier_piety_effect = yes
		}
		remove_artifact_modifier = artifact_placeholder_modifier
	}
}

create_artifact_pedestal_crozier_effect = {
	$OWNER$ = { save_scope_as = owner }
	$CREATOR$ = { save_scope_as = creator }

	get_artifact_quality_effect = yes
	get_artifact_wealth_effect = yes

	scope:owner = {
		create_artifact = {
			name = artifact_pedestal_crozier_name
			creator = scope:creator
			description = artifact_pedestal_crozier_description
			visuals = pam_crozier
			type = pedestal
			template = cross_template
			modifier = artifact_placeholder_modifier
			save_scope_as = newly_created_artifact
			wealth = scope:wealth
			quality = scope:quality
		}
	}

	scope:newly_created_artifact = {
		add_scaled_artifact_modifier_devotion_effect = yes
		add_scaled_artifact_modifier_grandeur_small_effect = yes
		if = {
			limit = {
				OR = {
					rarity = famed
					rarity = illustrious
				}
			}
			add_scaled_artifact_modifier_piety_effect = yes
		}
		remove_artifact_modifier = artifact_placeholder_modifier
	}
}

create_christian_religious_artifact_effect = {
	hidden_effect_new_object = {
		random_list = {
			1 = {
				create_artifact_pedestal_cross_effect = { OWNER = $OWNER$ CREATOR = $CREATOR$ }
			}
			1 = {
				trigger = {
					rite = {
						NOT = { rite_has_tenet = tenet_aniconism }
					}
				}
				create_artifact_pedestal_crucifix_effect = { OWNER = $OWNER$ CREATOR = $CREATOR$ }
			}
			1 = {
				create_artifact_sculpture_religious_effect = { OWNER = $OWNER$ CREATOR = $CREATOR$ }
			}
			1 = {
				create_artifact_pedestal_bible_effect = { OWNER = $OWNER$ CREATOR = $CREATOR$ }
			}
			1 = {
				create_artifact_pedestal_thurible_effect = { OWNER = $OWNER$ CREATOR = $CREATOR$ }
			}
			1 = {
				create_artifact_pedestal_situla_effect = { OWNER = $OWNER$ CREATOR = $CREATOR$ }
			}
			1 = {
				create_artifact_pedestal_crozier_effect = { OWNER = $OWNER$ CREATOR = $CREATOR$ }
			}
		}
		if = { # Makes a regular character artifact if you don't have a Royal Court
			limit = {
				has_royal_court = no
			}
			scope:newly_created_artifact = { reforge_artifact = { type = miscellaneous } }
		}
	}
}

# Called from wars on_white_peace
pam_miles_christi_white_peace_effect = {
	scope:defender = {
		if = {
			limit = {
				rite = { rite_has_parameter = tenet_miles_christi_white_peace_defender_bonus }
				scope:attacker = {
					faith = scope:defender.faith
				}
			}
			add_piety = {
				value = medium_piety_gain
				multiply = scope:attacker.primary_title.tier
				multiply = scope:defender.primary_title.tier
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_value
			add_legitimacy = {
				value = medium_legitimacy_gain
				multiply = scope:attacker.primary_title.tier
				multiply = scope:defender.primary_title.tier
			}
		}
	}
}
# Both on_combat_end_loser and on_combat_end_winner fire this
pam_miles_christi_fulfillment_from_combat_effect = {
	if = {
		limit = {
			rite = { rite_has_parameter = tenet_miles_christi_combat_fulfillment }
		}
		send_interface_toast = {
			title = successful_battles_and_sieges_give_sp_toast
			left_icon = root
			change_spiritual_fulfillment = minor_spiritual_fulfillment_value
		}
	}
}

# Called from on_combat_end_winner in the scope of the winning combat side.
pam_miles_christi_holy_weapon_victory_effect = {
	if = {
		limit = {
			exists = enemy_side.side_primary_participant
		}
		enemy_side.side_primary_participant = {
			save_temporary_scope_as = pam_holy_weapon_enemy
		}
		every_side_commander = {
			limit = {
				rite = { rite_has_parameter = tenet_miles_christi_holy_weapon_blessing }
				NOT = { faith.religion = scope:pam_holy_weapon_enemy.faith.religion }
			}
			every_character_artifact = {
				limit = { pam_holy_weapon_trigger = yes }
				pam_miles_christi_holy_weapon_gain_victory_effect = yes
			}
		}
	}
}

# Artifact scope. Tallies one holy victory and steps up the blessing when a threshold is crossed.
pam_miles_christi_holy_weapon_gain_victory_effect = {
	if = {
		limit = { NOT = { has_variable = pam_holy_weapon_victories_var } }
		set_variable = {
			name = pam_holy_weapon_victories_var
			value = 1
		}
	}
	else = {
		change_variable = {
			name = pam_holy_weapon_victories_var
			add = 1
		}
	}

	if = {
		limit = {
			NOT = { has_variable = pam_holy_weapon_blessing_var }
			var:pam_holy_weapon_victories_var >= 5
		}
		set_variable = {
			name = pam_holy_weapon_blessing_var
			value = 1
		}
		pam_miles_christi_holy_weapon_blessing_effect = { MODIFIER = pam_holy_weapon_1_artifact_modifier }
	}
	else_if = {
		limit = {
			has_variable = pam_holy_weapon_blessing_var
			var:pam_holy_weapon_blessing_var = 1
			var:pam_holy_weapon_victories_var >= 15
		}
		change_variable = {
			name = pam_holy_weapon_blessing_var
			add = 1
		}
		pam_miles_christi_holy_weapon_blessing_effect = { MODIFIER = pam_holy_weapon_2_artifact_modifier }
	}
	else_if = {
		limit = {
			has_variable = pam_holy_weapon_blessing_var
			var:pam_holy_weapon_blessing_var = 2
			var:pam_holy_weapon_victories_var >= 30
		}
		change_variable = {
			name = pam_holy_weapon_blessing_var
			add = 1
		}
		pam_miles_christi_holy_weapon_blessing_effect = { MODIFIER = pam_holy_weapon_3_artifact_modifier }
	}
	else_if = {
		limit = {
			has_variable = pam_holy_weapon_blessing_var
			var:pam_holy_weapon_blessing_var = 3
			var:pam_holy_weapon_victories_var >= 50
		}
		change_variable = {
			name = pam_holy_weapon_blessing_var
			add = 1
		}
		pam_miles_christi_holy_weapon_blessing_effect = { MODIFIER = pam_holy_weapon_4_artifact_modifier }
	}
}

# Artifact scope
# MODIFIER = the artifact modifier for the tier being reached
pam_miles_christi_holy_weapon_blessing_effect = {
	save_temporary_scope_as = pam_holy_weapon
	add_artifact_modifier = $MODIFIER$
	artifact_owner ?= {
		send_interface_toast = {
			type = event_toast_effect_good
			title = pam_holy_weapon_blessing_toast
			desc = pam_holy_weapon_blessing_toast_desc
			left_icon = this
			right_icon = scope:pam_holy_weapon
			show_as_tooltip = {
				scope:pam_holy_weapon = {
					add_artifact_modifier = $MODIFIER$
				}
			}
		}
	}
}

pam_pursuit_of_power_war_victory_fulfillment_effect = {
	scope:winner = {
		if = {
			limit = {
				has_personal_tenet_flag = tenet_pursuit_of_power_war_victory_fulfillment
				scope:war.casus_belli = {
					any_target_title = { is_landless_type_title = no }
				}
			}
			send_interface_toast = {
				title = pam_pursuit_of_power_war_victory_toast
				left_icon = root
				change_spiritual_fulfillment = medium_spiritual_fulfillment_value
			}
		}
	}
}

# Only rewards paying for someone else's freedom in gold; self-ransoms and favour settlements do not count.
pam_philanthropia_ransom_payer_effect = {
	scope:payer = {
		if = {
			limit = {
				has_personal_tenet_flag = tenet_philanthropia_ransom_captives
				this != scope:prisoner
				pam_philanthropia_ransom_gold_value > 0
			}
			send_interface_toast = {
				title = pam_philanthropia_ransom_toast
				left_icon = scope:prisoner
				add_piety = pam_philanthropia_ransom_piety_value
				change_spiritual_fulfillment = pam_philanthropia_ransom_fulfillment_value
			}
		}
	}
}

# Rewards releasing or exiling a prisoner outright; anything extracted from them first makes it a bargain.
tenet_benevolent_governance_clemency_effect = {
	scope:puppet_or_actor = {
		if = {
			limit = {
				has_personal_tenet_flag = tenet_benevolent_governance_clemency_fulfillment
				OR = {
					tenet_benevolent_governance_unconditional_release_trigger = yes
					AND = {
						scope:banish = yes
						has_execute_reason = scope:recipient
					}
				}
			}
			if = {
				limit = { is_ai = no }
				send_interface_toast = {
					type = event_generic_good
					title = benevolent_governance_clemency_fulfillment_toast
					left_icon = scope:puppet_or_actor
					right_icon = scope:recipient
					change_spiritual_fulfillment = tenet_benevolent_governance_clemency_fulfillment_value
				}
			}
			else = {
				change_spiritual_fulfillment = tenet_benevolent_governance_clemency_fulfillment_value
			}
		}
	}
}

pam_be_fruitful_fruitful_upbringing_effect = {
	if = {
		limit = {
			exists = scope:father
			OR = {
				scope:mother ?= { has_personal_tenet_flag = tenet_be_fruitful_and_multiply_fruitful_upbringing }
				scope:father ?= { has_personal_tenet_flag = tenet_be_fruitful_and_multiply_fruitful_upbringing }
			}
		}
		scope:child = {
			if = {
				limit = {
					scope:mother ?= { has_relation_soulmate = scope:father }
				}
				add_character_modifier = {
					modifier = tenet_be_fruitful_and_multiply_fruitful_upbringing_devoted
					years = 16
				}
			}
			else_if = {
				limit = {
					scope:mother ?= {
						OR = {
							has_relation_lover = scope:father
							has_relation_best_friend = scope:father
						}
					}
				}
				add_character_modifier = {
					modifier = tenet_be_fruitful_and_multiply_fruitful_upbringing_close
					years = 16
				}
			}
			else_if = {
				limit = {
					scope:mother ?= {
						OR = {
							has_relation_friend = scope:father
							opinion = {
								target = scope:father
								value >= medium_positive_opinion
							}
						}
					}
				}
				add_character_modifier = {
					modifier = tenet_be_fruitful_and_multiply_fruitful_upbringing_fond
					years = 16
				}
			}
		}
	}
}

# PAM Scholasticism tenet effect
pam_tenet_scholasticism_university_bonus_effect = {
	if = {
		limit = {
			rite = { rite_has_parameter = tenet_scholasticism_university_bonus }
		}
		add_character_modifier = {
			modifier = tenet_scholasticism_modifier
			years = 20
		}
	}
}

pam_pentarchy_circuit_entourage_effect = {
	if = {
		limit = { exists = involved_activity }
		save_temporary_scope_as = pam_pentarchy_circuit_pilgrim
		involved_activity = {
			every_attending_character = {
				limit = { this != scope:pam_pentarchy_circuit_pilgrim }
				custom = pam_pentarchy_circuit_entourage_tt
				hidden_effect = {
					local_court_type_skill_point_effect = {
						PROVINCE = scope:pam_pentarchy_circuit_pilgrim.location
					}
				}
			}
		}
	}
}

# Pentarchy tenet is already checked when pilgrimage_additional_reward_effect fires this
pam_pentarchy_pilgrim_circuit_effect = {
	save_temporary_scope_as = pam_pentarchy_pilgrim
	if = {
		limit = {
			NOT = { has_character_flag = pam_pentarchy_circuit_completed }
			exists = scope:pam_pentarchy_pilgrim.location.barony
			faith = {
				any_holy_site_barony = { this = scope:pam_pentarchy_pilgrim.location.barony }
			}
		}
		scope:pam_pentarchy_pilgrim.location.barony = {
			save_temporary_scope_as = pam_pentarchy_see
		}
		if = {
			limit = {
				OR = {
					NOT = { has_variable_list = pam_pentarchy_sees_visited }
					NOT = {
						is_target_in_variable_list = {
							name = pam_pentarchy_sees_visited
							target = scope:pam_pentarchy_see
						}
					}
				}
			}
			add_to_variable_list = {
				name = pam_pentarchy_sees_visited
				target = scope:pam_pentarchy_see
			}

			# Recount vs current holy sites so a conversion since visiting self-corrects
			set_variable = {
				name = pam_pentarchy_sees_total
				value = 0
			}
			set_variable = {
				name = pam_pentarchy_sees_count
				value = 0
			}
			faith = {
				every_holy_site = {
					limit = { exists = holy_site_barony }
					holy_site_barony = { save_temporary_scope_as = pam_pentarchy_counted_see }
					scope:pam_pentarchy_pilgrim = {
						change_variable = {
							name = pam_pentarchy_sees_total
							add = 1
						}
						if = {
							limit = {
								is_target_in_variable_list = {
									name = pam_pentarchy_sees_visited
									target = scope:pam_pentarchy_counted_see
								}
							}
							change_variable = {
								name = pam_pentarchy_sees_count
								add = 1
							}
						}
					}
				}
			}

			if = {
				limit = {
					# The log entry's description is drawn before the counters above are set
					has_variable = pam_pentarchy_sees_total
					var:pam_pentarchy_sees_total > 0
					var:pam_pentarchy_sees_count >= var:pam_pentarchy_sees_total
				}
				add_character_flag = pam_pentarchy_circuit_completed
				send_interface_toast = {
					type = event_toast_text_and_effect_good
					title = pam_pentarchy_circuit_completed_toast
					desc = pam_pentarchy_circuit_completed_toast_desc
					left_icon = scope:pam_pentarchy_pilgrim
					add_character_modifier = {
						modifier = pam_pilgrim_of_every_holy_site_modifier
					}
					add_piety = massive_piety_gain
					change_spiritual_fulfillment = major_spiritual_fulfillment_value
					if = {
						limit = { has_personal_tenet_flag = tenet_pentarchy_pilgrim_circuit_companions }
						pam_pentarchy_circuit_entourage_effect = yes
					}
				}
			}
			else = {
				send_interface_message = {
					type = event_generic_good_with_text
					title = pam_pentarchy_see_visited_message
					desc = pam_pentarchy_see_visited_message_desc
					left_icon = scope:pam_pentarchy_pilgrim
					add_piety = medium_piety_gain
					change_spiritual_fulfillment = minor_spiritual_fulfillment_value
					if = {
						limit = { has_personal_tenet_flag = tenet_pentarchy_pilgrim_circuit_companions }
						pam_pentarchy_circuit_entourage_effect = yes
					}
				}
			}
		}
	}
}

pam_tenet_scholasticism_yearly_effect = {
	random = {
		chance = {
			value = 5
			if = {
				limit = {
					is_ai = no
				}
				add = 5
			}
			if = {
				limit = {
					learning >= 30
				}
				add = 2
			}
			if = {
				limit = {
					learning >= 40
				}
				add = 3
			}
			if = {
				limit = {
					piety_level >= 4
				}
				add = 2
			}
			if = {
				limit = {
					piety_level >= 5
				}
				add = 3
			}
			if = {
				limit = {
					num_of_known_languages >= 3
				}
				add = 2
			}
			if = {
				limit = {
					num_of_known_languages >= 5
				}
				add = 3
			}
		}
		send_interface_toast = {
			type = event_toast_effect_good
			title = pam_tenet_scholasticism_toast_title
			left_icon = root
			add_character_modifier = {
				modifier = tenet_scholasticism_modifier
				years = 20
			}
		}
	}
}

pam_tenet_scholasticism_studium_effect = {
	save_scope_as = studium_master
	current_travel_plan ?= {
		random_entourage_character = {
			limit = {
				NOT = { this = scope:studium_master }
				is_physically_able_ai_adult = yes
				ep1_is_valid_character_for_inspiration_trigger = yes
			}
			random = {
				chance = pam_scholasticism_studium_inspiration_chance_value
				create_inspiration = book_inspiration
				save_scope_as = pam_studium_scholar
				scope:studium_master = {
					send_interface_toast = {
						type = event_toast_effect_good
						title = pam_tenet_scholasticism_studium_toast_title
						desc = pam_tenet_scholasticism_studium_toast_desc
						left_icon = scope:pam_studium_scholar
					}
				}
			}
		}
	}
}
add_scholar_trait_or_xp_effect = {
	if = {
		limit = {
			NOT = { has_trait = lifestyle_scholar }
		}
		add_trait = lifestyle_scholar
	}
	else = {
		add_trait_xp = {
			trait = lifestyle_scholar
			value = $VALUE$
		}
	}
}

save_most_suitable_monastic_leader_effect = {
	#Patronized by puppet_or_actor
	random_faith_holy_order = {
		limit = {
			take_the_vows_valid_monastic_order_trigger = { CHARACTER = scope:puppet_or_actor }
			holy_order_patron = scope:puppet_or_actor
		}
		leader = {
			save_scope_as = archabbot
		}
	}
	#leases from puppet_or_actor
	if = {
		limit = {
			NOT = {
				exists = scope:archabbot
			}
		}
		random_faith_holy_order = {
			limit = {
				take_the_vows_valid_monastic_order_trigger = { CHARACTER = scope:puppet_or_actor }
				any_leased_title = {
					county.holder ?= scope:puppet_or_actor
				}
			}
			leader = {
				save_scope_as = archabbot
			}
		}
	}
	#patronized by puppet_or_actor's superior
	if = {
		limit = {
			NOT = {
				exists = scope:archabbot
			}
		}
		random_faith_holy_order = {
			limit = {
				take_the_vows_valid_monastic_order_trigger = { CHARACTER = scope:puppet_or_actor }
				holy_order_patron = {
					is_liege_or_above_of = scope:puppet_or_actor
				}
			}
			leader = {
				save_scope_as = archabbot
			}
		}
	}
	#in same realm as puppet_or_actor
	if = {
		limit = {
			NOT = {
				exists = scope:archabbot
			}
		}
		random_faith_holy_order = {
			limit = {
				take_the_vows_valid_monastic_order_trigger = { CHARACTER = scope:puppet_or_actor }
				any_leased_title = {
					county.holder.top_liege ?= scope:puppet_or_actor.top_liege
				}
			}
			leader = {
				save_scope_as = archabbot
			}
		}
	}
	#in diplo range of puppet_or_actor
	if = {
		limit = {
			NOT = {
				exists = scope:archabbot
			}
		}
		random_faith_holy_order = {
			limit = {
				take_the_vows_valid_monastic_order_trigger = { CHARACTER = scope:puppet_or_actor }
				leader = {
					in_diplomatic_range = scope:puppet_or_actor
				}
			}
			leader = {
				save_scope_as = archabbot
			}
		}
	}
	#any order
	if = {
		limit = {
			NOT = {
				exists = scope:archabbot
			}
		}
		random_faith_holy_order = {
			limit = {
				take_the_vows_valid_monastic_order_trigger = { CHARACTER = scope:puppet_or_actor }
			}
			leader = {
				save_scope_as = archabbot
			}
		}
	}
}

# Saeculum Obscurum ends - time to get divorced
pam_the_christian_church_saeculum_obscurum_end_clergy_divorce_effect = {
	every_situation_participant = {
		limit = {
			is_clergy = yes
			rite = { rite_has_doctrine = doctrine_clerical_marriage_disallowed }
		}
		hidden_effect = {
			if = {
				limit = {
					is_married = yes
					is_ai = yes
				}
				every_spouse = {
					limit = { is_ai = yes }
					save_temporary_scope_as = saeculum_clergy_divorcee
					less_verbose_divorce_effect = {
						DIVORCER = prev
						DIVORCEE = scope:saeculum_clergy_divorcee
					}
				}
			}
			if = {
				limit = { exists = betrothed }
				break_betrothal = betrothed
			}
		}
	}
}

#The Christian Church phase start effect -
# Phase on_start runs with situation as root; every_player does not iterate players from that context. Iterate situation participants instead (cf. the_great_steppe phase on_start).
the_christian_church_fire_event_for_participant_players_effect = {
	if = {
		limit = {
			NOT = { has_global_variable = pam_skip_tcc_phase_start_event }
		}
		every_player = {
			limit = {
				# Saeculum Obscurum on_start fires the schism start event
				trigger_if = {
					limit = {
						situation:the_christian_church ?= { situation_current_phase = saeculum_obscurum }
					}
					situation:the_christian_church ?= {
						NOT = { has_variable = pam_great_schism_status }
					}
				}
				OR = {
					government_has_flag = government_is_theocracy
					years_from_game_start >= 1
				}
				any_character_situation = {
					this = situation:the_christian_church
				}
			}
			trigger_event = $EVENT$
		}
	}
}

#The Christian Church situation effects
the_christian_church_fire_correct_phase_start_event_for_player_effect = {
	if = {
		limit = {
			is_ai = no
			is_player_tutorial_character = no
			NOT = { has_global_variable = pam_skip_tcc_phase_start_event }
			OR = {
				government_has_flag = government_is_theocracy
				years_from_game_start >= 1
			}
		}
		save_scope_as = situation_participant
		situation:the_christian_church = {
			switch = {
				trigger = situation_current_phase
				fragile_unity = {
					scope:situation_participant = {
						trigger_event = pam_the_christian_church.0051
					}
				}
				saeculum_obscurum = {
					scope:situation_participant = {
						trigger_event = pam_the_christian_church.0052
					}
				}
				conversion_phase = {
					scope:situation_participant = {
						trigger_event = pam_the_christian_church.0053
					}
				}
				reform_phase = {
					scope:situation_participant = {
						trigger_event = pam_the_christian_church.0054
					}
				}
				investiture_crisis = {
					scope:situation_participant = {
						trigger_event = pam_the_christian_church.0055
					}
				}
				crusade_phase = {
					scope:situation_participant = {
						trigger_event = pam_the_christian_church.0056
					}
				}
				concord = {
					scope:situation_participant = {
						trigger_event = pam_the_christian_church.0057
					}
				}
			}
		}
	}
}

pam_remove_forbidden_personal_tenet_secret_if_invalid_effect = {
	if = {
		limit = {
			any_secret = { type = secret_forbidden_personal_tenet }
			NOT = { pam_has_forbidden_personal_tenet_trigger = yes }
		}
		every_secret = {
			type = secret_forbidden_personal_tenet
			remove_secret = yes
		}
	}
}

# Faith scope
pam_faith_cleanup_forbidden_personal_tenet_secrets_effect = {
	hidden_effect = {
		every_faith_character = {
			pam_remove_forbidden_personal_tenet_secret_if_invalid_effect = yes
		}
	}
}

# Faith scope. TENET is the tenet just prohibited
pam_faith_grant_forbidden_personal_tenet_secrets_effect = {
	hidden_effect = {
		every_faith_character = {
			limit = {
				has_personal_tenet = $TENET$
				NOT = { any_secret = { type = secret_forbidden_personal_tenet } }
			}
			on_personal_tenet_gain_secret_gain_effect = yes
		}
	}
}

on_personal_tenet_gain_secret_gain_effect = {
	# AI on_personal_tenet_gain is delayed, so the prohibited tenet may already be gone
	if = {
		limit = { pam_has_forbidden_personal_tenet_trigger = yes }
		send_interface_toast = {
			title = on_personal_tenet_gain_secret_gain_effect_tt
			left_icon = root
			add_secret = {
				type = secret_forbidden_personal_tenet
				target = this
			}
			if = {
				limit = { exists = var:tenet_evangelized_by }
				var:tenet_evangelized_by = { save_temporary_scope_as = tenet_evangelizer }
				every_secret = {
					type = secret_forbidden_personal_tenet
					reveal_to = scope:tenet_evangelizer
				}
				remove_variable = tenet_evangelized_by
			}
		}
	}
}

# Called from five_year_playable_pulse. Root is the character.
pam_ai_personal_tenet_pulse_effect = {
	if = {
		limit = {
			is_physically_able_ai = yes
		}

		# Phase 0: cardinals may hold beliefs their rite prohibits, so a new pontiff can arrive carrying one
		# Ahead of Phase 1 so the freed slot refills this pulse; on_personal_tenet_loss clears the secret
		if = {
			limit = {
				pam_holds_college_elected_church_seat_trigger = yes
				pam_has_forbidden_personal_tenet_trigger = yes
			}
			faith.main_rite = { save_temporary_scope_as = pam_pontiff_rite }
			while = {
				count = 8
				limit = { pam_has_forbidden_personal_tenet_trigger = yes }
				random_personal_tenet = {
					limit = {
						scope:pam_pontiff_rite = {
							has_tenet_status = {
								tenet = prev
								status = prohibited
							}
						}
					}
					root = { remove_personal_tenet = prev }
				}
			}
		}

		# Phase 1: fill every empty personal tenet slot. No chance gate - empty slots are wasted SF and immersion.
		# 8 = safety upper bound on slot cap (base 1 + up to ~5 from piety levels, with headroom).
		while = {
			count = 8
			limit = {
				free_personal_tenet_slots > 0
				any_desired_tenet = {
					only_positive = yes
					threshold = 1
					selection_count = 97
					save_temporary_scope_as = pam_personal_tenet_candidate
					NOT = {
						root = {
							any_personal_tenet = {
								this = scope:pam_personal_tenet_candidate
							}
						}
					}
					OR = {
						NOT = {
							root.faith.main_rite = {
								has_tenet_status = {
									tenet = scope:pam_personal_tenet_candidate
									status = prohibited
								}
							}
						}
						root = { pam_ai_can_consider_forbidden_personal_tenet_trigger = yes }
					}
				}
			}

			random_desired_tenet = {
				only_positive = yes
				threshold = 1
				selection_count = 97
				limit = {
					save_temporary_scope_as = pam_personal_tenet_candidate
					NOT = {
						root = {
							any_personal_tenet = {
								this = scope:pam_personal_tenet_candidate
							}
						}
					}
					OR = {
						NOT = {
							root.faith.main_rite = {
								has_tenet_status = {
									tenet = scope:pam_personal_tenet_candidate
									status = prohibited
								}
							}
						}
						root = { pam_ai_can_consider_forbidden_personal_tenet_trigger = yes }
					}
				}
				root = { set_personal_tenet = prev }
			}
		}

		# Phase 2: chance-gated swap, only when there are no free slots left to fill.
		if = {
			limit = {
				free_personal_tenet_slots <= 0
				any_desired_tenet = {
					only_positive = yes
					threshold = 1
					selection_count = 97
					save_temporary_scope_as = pam_personal_tenet_candidate
					NOT = {
						root = {
							any_personal_tenet = {
								this = scope:pam_personal_tenet_candidate
							}
						}
					}
					OR = {
						NOT = {
							root.faith.main_rite = {
								has_tenet_status = {
									tenet = scope:pam_personal_tenet_candidate
									status = prohibited
								}
							}
						}
						root = { pam_ai_can_consider_forbidden_personal_tenet_trigger = yes }
					}
				}
			}

			random = {
				chance = 30
				modifier = {
					add = 15
					has_trait = zealous
				}
				modifier = {
					add = -15
					has_trait = cynical
				}
				modifier = {
					add = {
						value = ai_zeal
						divide = 5
					}
				}
				modifier = {
					add = {
						value = ai_boldness
						divide = 5
					}
				}

				# Pick the new tenet - Random of any positive-SF known tenet, not strictly the best.
				random_desired_tenet = {
					only_positive = yes
					threshold = 1
					selection_count = 97
					limit = {
						save_temporary_scope_as = pam_personal_tenet_candidate
						NOT = {
							root = {
								any_personal_tenet = {
									this = scope:pam_personal_tenet_candidate
								}
							}
						}
						OR = {
							NOT = {
								root.faith.main_rite = {
									has_tenet_status = {
										tenet = scope:pam_personal_tenet_candidate
										status = prohibited
									}
								}
							}
							root = { pam_ai_can_consider_forbidden_personal_tenet_trigger = yes }
						}
					}
					root = { force_set_personal_tenet = prev }
				}
			}
		}
	}
}

conversion_tenet_acts_of_the_apostles_effect = {
	scope:puppet_or_actor = {
		if = {
			limit = { # Legitimacy if core to the faith
				rite = {
					rite_has_parameter = tenet_acts_of_apostles_legitimacy_from_converts
				}
				is_valid_for_legitimacy_change = yes
			}
			send_interface_toast = {
				title = conversion_tenet_acts_of_the_apostles_effect_legitimacy_tt
				left_icon = scope:puppet_or_actor
				add_legitimacy = minor_legitimacy_gain
			}
		}
		if = {
			limit = { # SP if personal tenet - converting the mighty pays more
				has_personal_tenet_flag = tenet_acts_of_apostles_fulfillment_from_converts
				scope:recipient ?= { is_landed = yes }
			}
			send_interface_toast = {
				title = conversion_tenet_acts_of_the_apostles_effect_sp_tt
				left_icon = scope:puppet_or_actor
				change_spiritual_fulfillment = major_spiritual_fulfillment_value
			}
		}
		else_if = {
			limit = {
				has_personal_tenet_flag = tenet_acts_of_apostles_fulfillment_from_converts
			}
			send_interface_toast = {
				title = conversion_tenet_acts_of_the_apostles_effect_sp_tt
				left_icon = scope:puppet_or_actor
				change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			}
		}
		if = {
			limit = { # Piety if core to the faith, scaled steeply by the convert's rank
				rite = { rite_has_parameter = tenet_acts_of_apostles_piety_from_converts }
				scope:recipient ?= { highest_held_title_tier >= tier_barony }
			}
			send_interface_toast = {
				title = conversion_tenet_acts_of_the_apostles_effect_piety_tt
				left_icon = scope:puppet_or_actor
				add_piety = pam_acts_conversion_piety_value
			}
		}
	}
	if = {
		limit = {
			scope:is_puppet_action ?= yes
			scope:puppet_or_actor = {
				rite = { rite_has_parameter = tenet_acts_of_apostles_legitimacy_from_converts }
				is_valid_for_legitimacy_change = yes
			}
		}
		scope:actor = {
			send_interface_toast = {
				title = conversion_tenet_acts_of_the_apostles_effect_legitimacy_tt_puppeteer
				left_icon = scope:puppet_or_actor
			}
		}
	}
	if = {
		limit = {
			scope:is_puppet_action ?= yes
			scope:puppet_or_actor = { has_personal_tenet_flag = tenet_acts_of_apostles_fulfillment_from_converts }
		}
		scope:actor = {
			send_interface_toast = {
				title = conversion_tenet_acts_of_the_apostles_effect_sp_tt_puppeteer
				left_icon = scope:puppet_or_actor
			}
		}
	}
}

pam_tax_nonbelievers_conversion_concession_effect = {
	if = {
		limit = {
			scope:tax_nonbelievers_conversion_concession = yes
		}
		scope:puppet_or_actor = {
			pay_short_term_gold = {
				target = scope:recipient
				gold = medium_gold_value
			}
			send_interface_toast = {
				type = event_toast_effect_good
				title = pam_tax_nonbelievers_conversion_concession_toast
				left_icon = scope:recipient
				right_icon = scope:puppet_or_actor
			}
		}
	}
}

conversion_tenet_mendicant_preachers_effect = {
	scope:puppet_or_actor = {
		if = {
			limit = {
				has_personal_tenet_flag = tenet_mendicant_preachers_conversion_fulfillment
			}
			send_interface_toast = {
				title = conversion_tenet_mendicant_preachers_effect_sp_tt
				left_icon = scope:puppet_or_actor
				change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			}
		}
	}
	if = {
		limit = {
			scope:is_puppet_action ?= yes
			scope:puppet_or_actor = { has_personal_tenet_flag = tenet_mendicant_preachers_conversion_fulfillment }
		}
		scope:actor = {
			send_interface_toast = {
				title = conversion_tenet_mendicant_preachers_effect_sp_tt_puppeteer
				left_icon = scope:puppet_or_actor
			}
		}
	}
}

pam_tax_nonbelievers_conversion_fulfillment_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tax_nonbelievers_fulfillment_from_conversion
		}
		change_spiritual_fulfillment = minor_spiritual_fulfillment_value
	}
}

ora_et_labora_gardener_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_ora_et_labora_gardener_trait
			NOT = { has_trait = lifestyle_gardener }
		}
		add_trait = lifestyle_gardener
	}
}

pam_tenet_takamin_hunter_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_takamin_hunter_trait
			NOT = { has_trait = lifestyle_hunter }
		}
		add_trait = lifestyle_hunter
	}
}

pam_tenet_religious_law_scholar_gain_effect = {
	if = {
		limit = {
			scope:tenet = tenet:tenet_religious_legal_pronouncements
			has_personal_tenet_flag = tenet_religious_law_scholar_trait
		}
		if = {
			limit = {
				NOT = { has_trait = lifestyle_scholar }
			}
			add_stress = medium_stress_gain
			add_trait = lifestyle_scholar
		}
	}
}

pam_tenet_filial_piety_education_completion_reward_effect = {
	if = {
		limit = {
			NOT = { has_character_flag = pam_filial_piety_education_rewarded }
			exists = scope:educator
			scope:educator = {
				is_parent_of = root
				has_personal_tenet_flag = tenet_filial_piety_education_completion_reward
			}
		}
		add_character_flag = pam_filial_piety_education_rewarded
		scope:educator = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = pam_filial_piety_education_completion_parent_toast
				left_icon = scope:educator
				right_icon = root
				add_piety = major_piety_value
				change_spiritual_fulfillment = major_spiritual_fulfillment_value
			}
		}
		send_interface_toast = {
			type = event_toast_effect_good
			title = pam_filial_piety_education_completion_child_toast
			left_icon = root
			right_icon = scope:educator
			add_piety = major_piety_value
			if = {
				limit = { is_playable_character = yes }
				change_spiritual_fulfillment = major_spiritual_fulfillment_value
			}
		}
	}
}

# An activity already under way was planned without the vow, so its reward is forfeit
pam_vows_of_poverty_block_ongoing_activity_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_vows_of_poverty_humble_host
			exists = involved_activity
			involved_activity.activity_host = root
		}
		involved_activity = {
			set_variable = pam_vows_of_poverty_completion_piety_paid
		}
	}
}

pam_vows_of_poverty_host_completion_piety_effect = {
	if = {
		limit = {
			this = scope:host
			scope:host = {
				has_personal_tenet_flag = tenet_vows_of_poverty_humble_host
			}
			scope:activity = {
				NOT = { has_variable = pam_vows_of_poverty_completion_piety_paid }
			}
		}
		scope:activity = {
			set_variable = pam_vows_of_poverty_completion_piety_paid
			scope:host = {
				send_interface_toast = {
					type = event_toast_effect_good
					title = pam_vows_of_poverty_activity_completion_piety_toast
					left_icon = scope:host
					add_piety = pam_vows_of_poverty_host_piety_value
				}
			}
		}
	}
}

hedonistic_reveler_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_hedonistic_reveler_trait
			NOT = { has_trait = lifestyle_reveler }
		}
		add_trait = lifestyle_reveler
	}
}

pam_tenet_mountain_worship_rough_terrain_expert_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_mountain_worship_rough_terrain_expert_trait
			NOT = { has_trait = rough_terrain_expert }
		}
		add_trait = rough_terrain_expert
	}
}

pam_tenet_mountain_worship_hike_spiritual_fulfillment_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_mountain_worship_hike
			location = {
				OR = {
					terrain = mountains
					terrain = desert_mountains
				}
			}
		}
		custom_tooltip = pam_tenet_mountain_worship_hike_spiritual_fulfillment_tt
		change_spiritual_fulfillment = medium_spiritual_fulfillment_value
	}
}

hedonistic_obesity_modifier_sync_effect = {
	if = {
		limit = {
			current_weight >= overweight_value
			has_personal_tenet_flag = tenet_hedonistic_obesity_fulfillment
		}
		if = {
			limit = {
				NOT = { has_character_modifier = tenet_hedonistic_obese_modifier }
			}
			add_character_modifier = {
				modifier = tenet_hedonistic_obese_modifier
			}
		}
	}
	else_if = {
		limit = {
			has_character_modifier = tenet_hedonistic_obese_modifier
		}
		remove_character_modifier = tenet_hedonistic_obese_modifier
	}
}

hedonistic_feast_gout_risk_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_hedonistic_feast_gout_risk
			current_weight >= overweight_value
			can_contract_disease_trigger = { DISEASE = gout_ridden }
		}
		random_list = {
			75 = {}
			25 = {
				send_interface_toast = {
					type = event_toast_effect_bad
					title = hedonistic_feast_gout_risk_toast
					left_icon = root
					show_as_tooltip = { add_trait_force_tooltip = gout_ridden }
					hidden_effect = {
						contract_disease_effect = { DISEASE = gout_ridden TREATMENT_EVENT = no }
					}
				}
			}
		}
	}
}

asceticism_inappetetic_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_asceticism_inappetetic_trait
			NOT = { has_trait = inappetetic }
		}
		add_trait = inappetetic
	}
}

# Traits granted by a personal tenet are permanent
mortification_holy_scars_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_mortification_holy_scars
			NOT = { has_trait = scarred }
		}
		add_trait = scarred
	}
}

# Revel in Pain belongs to Exaltation of Pain
pam_mortify_flesh_scourge_effect = {
	if = {
		limit = { has_personal_tenet_flag = tenet_mortification_revel_bonus }
		send_interface_toast = {
			type = event_generic_good
			title = pam_mortification_revel_toast
			left_icon = root
			if = { # If you don't have the trait, it's guaranteed
				limit = {
					NOT = { has_trait = flagellant }
				}
				add_trait = flagellant
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_value
		}
	}

	if = {
		limit = { has_personal_tenet_flag = tenet_mortification_holy_scars }
		send_interface_toast = {
			type = event_generic_good
			title = pam_mortification_holy_scars_toast
			left_icon = root
			add_trait = scarred
			add_trait_xp = {
				trait = scarred
				value = { integer_range = { min = 10 max = 25 } }
			}
		}
	}
}

# Wounds cannot kill here: the player chose this from a decision.
pam_mortify_flesh_offering_effect = {
	add_piety = massive_piety_value
	change_spiritual_fulfillment = major_spiritual_fulfillment_value
	stress_and_fulfillment_impact = {
		flagellant = medium_stress_impact_loss
		zealous = minor_stress_impact_loss
		craven = medium_stress_impact_gain
		content = medium_stress_impact_gain
	}
	increase_wounds_no_death_effect = { REASON = treatment }
}

literalism_scholar_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_literalism_scholar_trait
			NOT = { has_trait = lifestyle_scholar }
		}
		add_trait = lifestyle_scholar
	}
}

scholasticism_scholar_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_scholasticism_scholar_trait
			NOT = { has_trait = lifestyle_scholar }
		}
		add_trait = lifestyle_scholar
	}
	else_if = {
		limit = {
			has_personal_tenet_flag = tenet_scholasticism_scholar_trait
		}
		add_trait_xp = {
			trait = lifestyle_scholar
			value = pam_scholasticism_scholar_xp_value
		}
	}
}

unrelenting_faith_blademaster_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_unrelenting_faith_blademaster_trait
			NOT = { has_trait = lifestyle_blademaster }
		}
		add_trait = lifestyle_blademaster
	}
}

astrology_mystic_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_astrology_mystic_trait
			NOT = { has_trait = lifestyle_mystic }
		}
		add_trait = lifestyle_mystic
	}
}

anachoresis_mystic_gain_effect = {
	if = {
		limit = {
			scope:tenet = tenet:tenet_anachoresis
			has_personal_tenet_flag = tenet_anachoresis_mystic_trait
			NOT = { has_trait = lifestyle_mystic }
		}
		add_trait = lifestyle_mystic
	}
}

pure_land_aggressive_attacker_gain_effect = {
	if = {
		limit = {
			scope:tenet = tenet:tenet_pure_land
			has_personal_tenet_flag = tenet_pure_land_aggressive_attacker_trait
			NOT = { has_trait = aggressive_attacker }
		}
		add_trait = aggressive_attacker
	}
}

pure_land_prestige_level_loss_effect = {
	if = {
		limit = {
			scope:tenet = tenet:tenet_pure_land
			has_personal_tenet_flag = tenet_pure_land_prestige_level_loss
		}
		add_prestige_level = -1
	}
}

# Miles Christi personal tenet: the soldier of Christ takes up the mantle of a Holy Warrior
miles_christi_holy_warrior_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_miles_christi_holy_warrior_trait
			NOT = { has_trait = holy_warrior }
		}
		add_trait = holy_warrior
	}
}

# PAM - Reincarnation: claim a forebear's personality trait.
# Opposing traits are replaced first; otherwise the least-fulfilling personality trait is replaced.
pam_claim_remembered_personality_trait_effect = {
	if = {
		limit = {
			$TRAIT$ = {
				any_opposite_trait = {
					root = { has_trait = prev }
				}
			}
		}
		$TRAIT$ = {
			every_opposite_trait = {
				limit = {
					root = { has_trait = prev }
				}
				root = { remove_trait = prev }
			}
		}
	}
	else_if = {
		limit = { number_of_personality_traits >= 1 }
		ordered_character_trait = {
			limit = { has_trait_category = personality }
			order_by = pam_recall_worst_personality_order_value
			save_scope_as = pam_recall_replaced_personality
		}
		remove_trait = scope:pam_recall_replaced_personality
	}
	add_trait = $TRAIT$
	change_spiritual_fulfillment = medium_spiritual_fulfillment_value
}

pam_great_project_contribution_catalyst_effect = {
	if = {
		limit = {
			this != scope:owner
			this != scope:founder
			scope:owner ?= {
				any_character_situation = {
					situation_type = the_christian_church
				}
			}
			OR = {
				scope:great_project.great_project_type = great_project_type:pam_cathedral_01
				scope:great_project.great_project_type = great_project_type:pam_cathedral_02
				scope:great_project.great_project_type = great_project_type:pam_cathedral_03
				scope:great_project.great_project_type = great_project_type:pam_mass_rite_conversion
			}
		}
		random_character_situation = {
			limit = { situation_type = the_christian_church }
			switch = {
				trigger = scope:great_project.great_project_type
				great_project_type:pam_cathedral_01 = {
					if = {
						limit = { situation_top_has_catalyst = catalyst_great_project_cathedral_contribution }
						trigger_situation_catalyst = catalyst_great_project_cathedral_contribution
					}
				}
				great_project_type:pam_cathedral_02 = {
					if = {
						limit = { situation_top_has_catalyst = catalyst_great_project_cathedral_contribution }
						trigger_situation_catalyst = catalyst_great_project_cathedral_contribution
					}
				}
				great_project_type:pam_cathedral_03 = {
					if = {
						limit = { situation_top_has_catalyst = catalyst_great_project_cathedral_contribution }
						trigger_situation_catalyst = catalyst_great_project_cathedral_contribution
					}
				}
				great_project_type:pam_mass_rite_conversion = {
					if = {
						limit = { situation_top_has_catalyst = catalyst_great_project_mass_rite_conversion_contribution }
						trigger_situation_catalyst = catalyst_great_project_mass_rite_conversion_contribution
					}
				}
			}
		}
	}
}

pam_levy_the_tithes_decision_effects = {
	every_vassal = {
		limit = {
			OR = {
				government_has_flag = government_is_theocracy
				government_has_flag = government_is_ecclesiastical
			}
		}
		custom = every_theocratic_vassal_tt
		pay_short_term_gold = {
			target = root
			gold = minor_gold_value
		}
	}
	if = {
		limit = { exists = cp:councillor_court_chaplain }
		cp:councillor_court_chaplain = {
			progress_towards_rival_effect = {
				CHARACTER = root
				REASON = rival_levied_tithes
				OPINION = default_rival_opinion
			}
		}
	}
}

#The flag is set in give_cardinal_order_interaction this provides the appropriate event option to accept or deny, along with communicating to players (via toast) who rule the county(s) affected by the below modifiers that a change has taken place
pam_choose_cardinal_order_switch_scripted_effect = {
	switch = {
		trigger = yes
		scope:cardinal_improve_development ?= {
			capital_county.clerical_region_title = {
				every_county_in_clerical_region = {
					custom = cardinal_order_improve_development
					limit = {
						faith = scope:recipient.faith
					}
					add_county_modifier = { 
						modifier = pam_cardinal_order_improve_development_modifier
						years = 10
					}
					holder = {
						if = {
							limit = {
								is_ai = no
							}
							add_to_list = county_holders
						}	
					}
				}
			}
		}

		scope:cardinal_convert_faith ?= {
			capital_county ?= {
				clerical_region_title ?= {
					every_county_in_clerical_region = {
						custom = cardinal_order_convert_faith
						limit = {
							pam_cardinal_order_convert_faith_in_region_county_trigger = { CARDINAL = scope:recipient }
						}
						add_county_modifier = { 
							modifier = pam_cardinal_order_convert_faith_modifier
							years = 10
						}
						holder = {
							if = {
								limit = {
									is_ai = no
								}
								add_to_list = county_holders
							}	
						}
					}
					if = {
						limit = {
							any_county_in_clerical_region = {
								any_neighboring_county = {
									pam_cardinal_order_convert_faith_neighboring_county_trigger = { CARDINAL = scope:recipient }
								}
							}
						}
						custom_tooltip = {
							text = cardinal_order_convert_faith_neighboring
							every_county_in_clerical_region = {
								every_neighboring_county = {
									limit = {
										pam_cardinal_order_convert_faith_neighboring_county_trigger = { CARDINAL = scope:recipient }
										NOT = { has_county_modifier = pam_cardinal_order_convert_faith_modifier }
									}
									add_county_modifier = { 
										modifier = pam_cardinal_order_convert_faith_modifier
										years = 10
									}
									holder = {
										if = {
											limit = {
												is_ai = no
											}
											add_to_list = county_holders
										}	
									}
								}
							}
						}
					}
				}
			}
		}

		scope:cardinal_increase_control ?= {
			capital_county.clerical_region_title = {
				every_county_in_clerical_region = {
					custom = cardinal_order_increase_control
					limit = {
						holder.top_liege.faith = scope:recipient.faith
					}
					add_county_modifier = { 
						modifier = pam_cardinal_order_increase_control_modifier
						years = 10
					}
					holder = {
						if = {
							limit = {
								is_ai = no
							}
							add_to_list = county_holders
						}	
					}
				}
			}
		}
		
		scope:cardinal_accelerate_cultural_fascination ?= {
			scope:puppet_or_actor.culture.culture_head = {
				custom_tooltip = cardinal_order_accelerate_cultural_fascination
				add_character_modifier = { 
					modifier = pam_cardinal_order_accelerate_cultural_fascination_modifier
					years = 10
				}
				if = {
					limit = {
						is_ai = no
					}
					add_to_list = county_holders
				}	
			}
		}

		scope:cardinal_improve_epidemic_resistance ?= {
			capital_county.clerical_region_title = {
				every_county_in_clerical_region = {
					custom = cardinal_order_improve_epidemic_resistance
					limit = {
						NOT = {
							has_county_modifier = pam_cardinal_order_improve_epidemic_resistance_modifier
						}
						faith = scope:recipient.faith
					}
					add_county_modifier = {
						modifier = pam_cardinal_order_improve_epidemic_resistance_modifier
						years = 10
					}
					holder = {
						if = {
							limit = {
								is_ai = no
							}
							add_to_list = county_holders
						}	
					}
				}
			}
		}
		
		scope:cardinal_improve_legitimacy ?= {
			capital_county.clerical_region_title = {
				every_county_in_clerical_region = {
					custom = cardinal_order_improve_legitimacy
					limit = {
						holder.top_liege = {
							has_legitimacy = yes
							NOT = {
								has_character_modifier = pam_cardinal_order_improve_legitimacy_modifier
							}
							faith = scope:recipient.faith
						}
					}
					holder.top_liege = {
						add_character_modifier = { 
							modifier = pam_cardinal_order_improve_legitimacy_modifier
							years = 10
						}
						if = {
							limit = {
								is_ai = no
							}
							add_to_list = county_holders
						}
					}
				}
			}
		}

		scope:cardinal_undermine_legitimacy ?= {
			capital_county.clerical_region_title = {
				every_county_in_clerical_region = {
					custom = cardinal_order_undermine_legitimacy
					limit = {
						holder.top_liege = {
							has_legitimacy = yes
							NOT = {
								has_character_modifier = pam_cardinal_order_undermine_legitimacy_modifier
							}
							faith = scope:recipient.faith
						}
					}
					holder.top_liege = {
						add_character_modifier = { 
							modifier = pam_cardinal_order_undermine_legitimacy_modifier
							years = 10
						}
						if = {
							limit = {
								is_ai = no
							}
							add_to_list = county_holders
						}
					}
				}
			}
		}
	}
}

pam_cardinal_order_accept_notify_players_effect = {
	$HOF$ = {
		if = {
			limit = { is_ai = no }
			hidden_effect = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = give_cardinal_order_interaction_toast
					left_icon = $HOF$
					right_icon = scope:recipient
					scope:recipient = {
						show_as_tooltip = {
							pam_choose_cardinal_order_switch_scripted_effect = yes
						}
					}
				}
			}
		}
	}
	if = {
		limit = {
			scope:actor != $HOF$
			scope:actor = { is_ai = no }
		}
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = give_cardinal_order_interaction_toast
				left_icon = $HOF$
				right_icon = scope:recipient
				scope:recipient = {
					show_as_tooltip = {
						pam_choose_cardinal_order_switch_scripted_effect = yes
					}
				}
			}
		}
	}
	every_in_list = {
		list = county_holders
		limit = {
			NOT = { this = $HOF$ }
			NOT = { this = scope:actor }
		}
		hidden_effect = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = give_cardinal_order_interaction_toast
				left_icon = $HOF$
				right_icon = scope:recipient
				scope:recipient = {
					show_as_tooltip = {
						pam_choose_cardinal_order_switch_scripted_effect = yes
					}
				}
			}
		}
	}
}

pam_cardinal_order_decline_effect = {
	$CARDINAL$ = {
		add_prestige = medium_prestige_gain
		add_piety = medium_piety_loss
		if = {
			limit = {
				$HOF$ = { is_alive = yes }
				can_set_relation_rival_trigger = { CHARACTER = $HOF$ }
			}
			progress_towards_rival_effect = {
				REASON = rival_ignored_orders
				CHARACTER = $HOF$
				OPINION = default_rival_opinion
			}
		}
		else_if = {
			limit = {
				$HOF$ = { is_alive = yes }
			}
			add_opinion = {
				target = $HOF$
				modifier = disrespect_opinion
				opinion = -60
			}
		}
	}
}

# Shared handler for the offer_pilgrimage send option: ACTOR promises the pilgrimage, CLERIC is rewarded
pam_offer_pilgrimage_effect = {
	if = {
		limit = {
			scope:offer_pilgrimage ?= yes
		}
		$ACTOR$ = {
			add_character_modifier = {
				modifier = promised_pilgrimage_to_clergy_modifier
				years = 10
			}
			custom_tooltip = actor_promised_pilgrimage_tt
		}
		$CLERIC$ = {
			add_piety_experience = {
				value = minor_piety_gain
				add = {
					value = $ACTOR$.highest_held_title_tier
					subtract = 1.5
					multiply = 20
				}
			}
		}
	}
}

request_legitimacy_interaction_effect = {
	add_legitimacy = {
		value = medium_legitimacy_gain
		multiply = 1.5
		if = {
			limit = {
				scope:monastic_support = yes
			}
			scope:puppet_or_actor = {
				every_patroned_holy_order = {
					limit = {
						leader = {
							government_has_flag = government_is_monastic_holy_order
						}
					}
					add = minor_legitimacy_gain
				}
			}
		}
		if = {
			limit = {
				scope:puppet_or_actor ?= {
					has_personal_tenet_flag = tenet_apostolic_succession_papal_legitimacy
					faith.religious_head ?= scope:recipient
				}
			}
			add = medium_legitimacy_gain
		}
		max = massive_legitimacy_gain
	}
	if = {
		limit = {
			rite = {
				has_tenet_status = {
					tenet = tenet_simony
					status = prohibited
				}
			}
		}
		change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
	}
	if = {
		limit = {
			any_vassal = {
				NOT = {
					faith = scope:puppet_or_actor.faith
				}
			}
		}
		every_vassal = {
			custom = custom.every_astray_faith_vassal
			limit = {
				NOT = {
					faith = scope:puppet_or_actor.faith
				}
				faith = {
					faith_hostility_level = {
						target = scope:puppet_or_actor.faith
						value >= faith_astray_level
					}
				}
			}
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = heathen_mandate_opinion
				opinion = -20
			}
		}
	}
	if = {
		limit = {
			any_held_title = {
				tier = tier_county
				NOT = {
					faith = scope:puppet_or_actor.faith
				}
				faith = {
					faith_hostility_level = {
						target = scope:puppet_or_actor.faith
						value >= faith_astray_level
					}
				}
			}
		}
		every_held_title = {
			custom = custom.every_astray_faith_domain_county
			limit = {
				tier = tier_county
				NOT = {
					faith = scope:puppet_or_actor.faith
				}
				faith = {
					faith_hostility_level = {
						target = scope:puppet_or_actor.faith
						value >= faith_astray_level
					}
				}
			}
			add_county_modifier = {
				modifier = heathen_mandate_modifier
				years = 5
			}
		}
	}
	if = {
		limit = {
			scope:monastic_support = yes
		}
		every_patroned_holy_order = {
			limit = {
				leader = {
					government_has_flag = government_is_monastic_holy_order
				}
			}
			scope:puppet_or_actor = {
				pay_short_term_gold = {
					target = prev.leader
					gold = minor_gold_value
				}
			}
		}
	}
	if = {
		limit = {
			scope:tithe = yes
		}
		every_held_title = {
			limit = {
				tier = tier_county
				NOT = {
					any_county_province = {
						is_occupied = yes
					}
				}
				development_level >= 1
			}
			add_to_list = tithed_counties
		}
		scope:recipient = {
			add_gold = {
				value = 0
				every_in_list = {
					list = tithed_counties
					add = scope:recipient.miniscule_gold_value
					add = {
						value = development_level
					}
				}
				max = monumental_gold_value
			}
		}
		every_in_list = {
			list = tithed_counties
			custom = custom.every_domain_county_1_plus_development
			if = {
				limit = {
					scope:recipient.culture = {
						has_cultural_era_or_later = culture_era_late_medieval
					}
				}
				change_development_progress = massive_development_progress_loss
			}
			else_if = {
				limit = {
					scope:recipient.culture = {
						has_cultural_era_or_later = culture_era_high_medieval
					}
				}
				change_development_progress = major_development_progress_loss
			}
			else = {
				change_development_progress = medium_development_progress_loss
			}
		}
		#Secretly take gold from wealthy-ass barons
		hidden_effect = {
			every_in_list = {
				list = tithed_counties
				every_county_province = {
					limit = {
						barony.holder ?= {
							highest_held_title_tier = tier_barony
							is_ai = yes
							gold > medium_gold_value
						}
					}
					barony.holder ?= {
						remove_short_term_gold = medium_gold_value
					}
				}
			}
		}
		if = {
			limit = {
				any_vassal = {
					has_vassal_stance = barons_and_minor_landholders
				}
			}
			every_vassal = {
				custom = custom.every_barons_and_minor_landholders_vassal
				limit = {
					has_vassal_stance = barons_and_minor_landholders
				}
				add_opinion = {
					modifier = tithed_opinion
					target = scope:puppet_or_actor
					opinion = -25
				}
			}
			custom_tooltip = tithe_takes_baron_gold_tt
		}
	}
	if = {
		limit = {
			scope:donate_faith = yes
		}
		pay_short_term_gold = {
			target = scope:recipient
			gold = scope:puppet_or_actor.major_gold_value
		}
	}
	if = {
		limit = {
			scope:use_hook = yes
			has_usable_hook = scope:recipient
		}
		use_hook = scope:recipient
	}
	if = {
		limit = {
			scope:offer_hook = yes
		}
		scope:recipient = {
			add_hook = {
				type = favor_hook
				target = scope:puppet_or_actor
			}
		}
	}
	if = {
		limit = {
			is_ai = yes
		}
		add_opinion = {
			target = scope:recipient
			modifier = grateful_opinion
			opinion = 30
		}
	}
	pam_offer_pilgrimage_effect = {
		ACTOR = scope:puppet_or_actor
		CLERIC = scope:recipient
	}
	#The cleric should lose spiritual fulfillment if they agree to supporting a wicked king or a tyrant
	if = {
		limit = {
			OR = {
				character_considered_wicked_trigger = { RITE = scope:recipient.rite }
				scope:recipient = {
					has_relation_rival = scope:puppet_or_actor
				}
				tyranny > medium_tyranny
				has_trait = excommunicated
			}
		}
		scope:recipient = {
			# We never take more piety than the cleric has, or the Church goes into debt
			add_piety = {
				value = major_piety_value
				multiply = {
					value = scope:puppet_or_actor.highest_held_title_tier
					subtract = 1
				}
				max = piety
				min = 0
				multiply = -1
			}
			change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
			if = {
				limit = {
					scope:puppet_or_actor = { has_trait = excommunicated }
				}
				custom_tooltip = sp_ful_loss_due_to_excommunicated
			}
			else_if = {
				limit = {
					has_relation_rival = scope:puppet_or_actor
				}
				custom_tooltip = sp_ful_loss_due_to_rival
			}
			else_if = {
				limit = {
					scope:puppet_or_actor = { tyranny > medium_tyranny }
				}
				custom_tooltip = sp_ful_loss_due_to_tyranny
			}
			else_if = {
				custom_tooltip = sp_ful_loss_due_to_wicked
			}
		}
	}
	else_if = {
		limit = {
			OR = {
				character_considered_morally_dubious_trigger = { RITE = scope:recipient.rite }
				tyranny >= low_tyranny
			}
		}
		scope:recipient = {
			add_piety = {
				value = minor_piety_value
				multiply = {
					value = scope:puppet_or_actor.highest_held_title_tier
					subtract = 1
				}
				max = piety
				min = 0
				multiply = -1
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
			if = {
				limit = {
					scope:puppet_or_actor = { tyranny >= low_tyranny }
				}
				custom_tooltip = sp_ful_loss_due_to_tyranny
			}
			else = {
				custom_tooltip = sp_ful_loss_due_to_immoral
			}
		}
	}
	else = {
		scope:recipient = {
			add_piety = {
				value = miniscule_piety_value
				multiply = {
					value = scope:puppet_or_actor.highest_held_title_tier
					subtract = 1
				}
				max = piety
				min = 0
				multiply = -1
			}
		}
	}
	# Catalysts
	if = {
		limit = {
			scope:puppet_or_actor.religion = religion:christianity_religion
			situation:the_christian_church ?= { situation_top_has_catalyst = catalyst_the_christian_church_requested_legitimacy }
		}
		situation:the_christian_church = { trigger_situation_catalyst = catalyst_the_christian_church_requested_legitimacy }
	}
}
#Add variable that makes title grant recipient (who is claimant) satisfied with claim-granting for a bit
claimant_jealousy_immunity_effect = {
	custom_tooltip = satisfied_claim_granting_tt
	if = {
		limit = {
			scope:highest_claim.tier = tier_hegemony
		}
		add_to_variable_list = {
			name = hegemony_grant
			target = scope:puppet_or_actor
			years = 3
		}
	}
	else_if = {
		limit = {
			scope:highest_claim.tier = tier_empire
		}
		add_to_variable_list = {
			name = empire_grant
			target = scope:puppet_or_actor
			years = 3
		}
	}
	else_if = {
		limit = {
			scope:highest_claim.tier = tier_kingdom
		}
		add_to_variable_list = {
			name = kingdom_grant
			target = scope:puppet_or_actor
			years = 3
		}
	}
	else_if = {
		limit = {
			scope:highest_claim.tier = tier_duchy
		}
		add_to_variable_list = {
			name = duchy_grant
			target = scope:puppet_or_actor
			years = 3
		}
	}
	else_if = {
		limit = {
			scope:highest_claim.tier = tier_county
		}
		add_to_variable_list = {
			name = county_grant
			target = scope:puppet_or_actor
			years = 3
		}
	}
}
claimant_rivalry_mercy_effect = {
	custom_tooltip = strong_claim_given_away_rivalry_tt
	if = {
		limit = {
			scope:highest_denied_claim = {
				tier >= tier_hegemony
			}
		}
		add_to_variable_list = {
			name = denied_hegemony
			target = scope:puppet_or_actor
			days = 31
		}
	}
	else_if = {
		limit = {
			scope:highest_denied_claim = {
				tier >= tier_empire
			}
		}
		add_to_variable_list = {
			name = denied_empire
			target = scope:puppet_or_actor
			days = 31
		}
	}
	else_if = {
		limit = {
			scope:highest_denied_claim = {
				tier >= tier_kingdom
			}
		}
		add_to_variable_list = {
			name = denied_kingdom
			target = scope:puppet_or_actor
			days = 31
		}
	}
	else_if = {
		limit = {
			scope:highest_denied_claim = {
				tier >= tier_duchy
			}
		}
		add_to_variable_list = {
			name = denied_duchy
			target = scope:puppet_or_actor
			days = 31
		}
	}
	else_if = {
		limit = {
			scope:highest_denied_claim = {
				tier >= tier_county
			}
		}
		add_to_variable_list = {
			name = denied_county
			target = scope:puppet_or_actor
			days = 31
		}
	}
	trigger_event = {
		id = pam_interactions_events.0040
		days = 30
	}
}
#Opinion loss with underlings who think they should get the title you're granting
title_granting_controversy_effect = {
	#populate vassal list
	every_vassal_or_below = {
		limit = {
			NOR = {
				this = scope:recipient
				this = $TITLE$.holder
				is_in_list = eligible_vassals
			}
			highest_held_title_tier > tier_barony
			#Vassal granting should only annoy people who could actually receive vassal
			trigger_if = {
				limit = {
					exists = scope:secondary_actor
				}
				highest_held_title_tier > $TITLE$.tier
			}
			#Children can't hold titles in admin govs
			trigger_if = {
				limit = {
					government_has_mechanic = administrative
				}
				is_adult = yes
				#Admin de jure/neighboring thing is a pain in the butt but should be acknowledged
				trigger_if = {
					limit = {
						is_ruler = yes
					}
					any_held_title = {
						OR = {
							# The target title is part of your de jure structure
							is_de_jure_liege_or_above_target = $TITLE$
							target_is_de_jure_liege_or_above = $TITLE$
						}
					}
				}
			}
			scope:puppet_or_actor = {
				is_character_interaction_valid = {
					recipient = prev
					interaction = $INTERACTION$
				}
			}
			#Shouldn't lose opinion when giving title to their child or parent
			NOR = {
				AND = {
					is_child_of = scope:recipient
					is_heir_of = scope:recipient
				}
				AND = {
					is_parent_of = scope:recipient
					exists = scope:recipient.house
					house ?= scope:recipient.house
				}
			}
		}
		add_to_list = eligible_vassals
	}
	#Also show effect on courtier claimants you actually care about (IE your family members)
	$TITLE$ = {
		every_claimant = {
			limit = {
				NOT = { this = scope:recipient }
				NOT = {
					is_in_list = eligible_vassals
				}
				#Vassal granting should only annoy people who could actually receive vassal
				trigger_if = {
					limit = {
						exists = scope:secondary_actor
					}
					highest_held_title_tier > $TITLE$.tier
				}
				trigger_if = {
					limit = {
						is_ruler = no
					}
					OR = {
						rite = {
							rite_has_parameter = gender_equal_law
						}
						AND = {
							is_male = yes
							rite = {
								rite_has_parameter = male_dominated_law
							}
						}
						AND = {
							is_female = yes
							rite = {
								rite_has_parameter = female_dominated_law
							}
						}
					}
				}
				in_diplomatic_range = scope:puppet_or_actor
				NOT = { has_strong_implicit_claim_on = $TITLE$ }
				any_claim = {
					pressed = yes
					this = $TITLE$
				}
				any_liege_or_above = {
					this = scope:puppet_or_actor
				}
				OR = {
					any_close_family_member = {
						this = scope:puppet_or_actor
					}
					AND = {
						exists = scope:puppet_or_actor.house
						house ?= scope:puppet_or_actor.house
					}
					any_spouse = {
						this = scope:puppet_or_actor
					}
					has_any_good_relationship_with_character_trigger = { CHARACTER = scope:puppet_or_actor }
				}
				#Children can't hold titles in admin govs
				trigger_if = {
					limit = {
						government_has_mechanic = administrative
					}
					is_adult = yes
					#Admin de jure/neighboring thing is a pain in the butt but should be acknowledged
					trigger_if = {
						limit = {
							is_ruler = yes
						}
						any_held_title = {
							OR = {
								# The target title is part of your de jure structure
								is_de_jure_liege_or_above_target = $TITLE$
								target_is_de_jure_liege_or_above = $TITLE$
							}
						}
					}
				}
				scope:puppet_or_actor = {
					is_character_interaction_valid = {
						recipient = prev
						interaction = $INTERACTION$
					}
				}
				#Shouldn't lose opinion when giving title to their child or parent
				NOR = {
					AND = {
						is_child_of = scope:recipient
						is_heir_of = scope:recipient
					}
					AND = {
						is_parent_of = scope:recipient
						exists = scope:recipient.house
						house ?= scope:recipient.house
					}
				}
			}
			add_to_list = eligible_vassals
		}
	}

	#Higher repercussions for higher tier
	#Higher repercussions for pressed claims than unpressed
	if = {
		limit = {
			$TITLE$ = {
				tier >= tier_kingdom
			}
		}
		every_in_list = {
			list = eligible_vassals
			limit = {
				NOT = { has_strong_implicit_claim_on = $TITLE$ }
				has_claim_on = $TITLE$
				#Wasn't granted a claim of same tier or better in recent memory
				NOR = {
					any_in_list = {
						variable = hegemony_grant
						this = scope:puppet_or_actor
					}
					AND = {
						$TITLE$.tier <= tier_empire
						any_in_list = {
							variable = empire_grant
							this = scope:puppet_or_actor
						}
					}
					AND = {
						$TITLE$.tier <= tier_kingdom
						any_in_list = {
							variable = kingdom_grant
							this = scope:puppet_or_actor
						}
					}
					AND = {
						$TITLE$.tier <= tier_duchy
						any_in_list = {
							variable = duchy_grant
							this = scope:puppet_or_actor
						}
					}
					AND = {
						$TITLE$.tier <= tier_county
						any_in_list = {
							variable = county_grant
							this = scope:puppet_or_actor
						}
					}
				}
			}
			#pressed claim
			if = {
				limit = {
					has_strong_claim_on = $TITLE$
				}
				add_to_list = strong_claimants
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_my_pressed_claim_opinion
					opinion = -50
				}
			}
			#unpressed claim
			else = {
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_my_unpressed_claim_opinion
					opinion = -30
				}
			}
		}
		#Upset they didn't get de jure vassal title
		every_in_list = {
			list = eligible_vassals
			limit = {
				any_held_title = {
					any_in_de_jure_hierarchy = {
						this = $TITLE$
					}
				}
			}
			#Less opinion loss if you're giving it to one of their vassals
			if = {
				limit = {
					OR = {
						exists = scope:secondary_actor
						any_vassal_or_below = {
							this = scope:recipient
						}
					}
				}
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_de_jure_vassal_title_opinion
					opinion = -25
				}
			}
			else = {
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_de_jure_vassal_title_opinion
					opinion = -40
				}
			}
		}
		#De Jure vassals upset they didn't get the title
		every_in_list = {
			list = eligible_vassals
			limit = {
				any_held_title = {
					tier >= $TITLE$.tier_minus_1
					any_this_title_or_de_jure_above = {
					 	this = $TITLE$
					}
				}
			}
			#Less opinion loss if you're giving it to their liege or above
			if = {
				limit = {
					any_liege_or_above = {
						this = scope:recipient
					}
				}
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_de_jure_liege_title_opinion
					opinion = -20
				}
			}
			else = {
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_de_jure_liege_title_opinion
					opinion = -30
				}
			}
		}
	}
	else_if = {
		limit = {
			#Either title is a duchy or we're doing vassal transfer, not title granting
			OR = {
				$TITLE$ = {
					tier >= tier_duchy
				}
				exists = scope:secondary_actor
			}
		}
		every_in_list = {
			list = eligible_vassals
			limit = {
				NOT = { has_strong_implicit_claim_on = $TITLE$ }
				any_claim = {
					this = $TITLE$
					pressed = yes
				}
			}
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = gave_away_my_pressed_claim_opinion
				opinion = -40
			}
		}
		every_in_list = {
			list = eligible_vassals
			limit = {
				NOT = { has_strong_implicit_claim_on = $TITLE$ }
				any_claim = {
					this = $TITLE$
					pressed = no
				}
			}
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = gave_away_my_unpressed_claim_opinion
				opinion = -20
			}
		}
		#Upset they didn't get de jure vassal title
		every_in_list = {
			list = eligible_vassals
			limit = {
				any_held_title = {
					any_in_de_jure_hierarchy = {
						this = $TITLE$
					}
				}
			}
			#Less opinion loss if you're giving it to one of their vassals or it's a vassal transfer
			if = {
				limit = {
					OR = {
						exists = scope:secondary_actor
						any_vassal_or_below = {
							this = scope:recipient
						}
					}
				}
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_de_jure_vassal_title_opinion
					opinion = -20
				}
			}
			else = {
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_de_jure_vassal_title_opinion
					opinion = -30
				}
			}
		}
		#De Jure vassals upset they didn't get the title
		every_in_list = {
			list = eligible_vassals
			limit = {
				any_held_title = {
					tier >= $TITLE$.tier_minus_1
					any_this_title_or_de_jure_above = {
					 	this = $TITLE$
					}
				}
			}
			#Less opinion loss if you're giving it to their liege or above
			if = {
				limit = {
					any_liege_or_above = {
						this = scope:recipient
					}
				}
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_de_jure_liege_title_opinion
					opinion = -15
				}
			}
			else = {
				add_opinion = {
					target = scope:puppet_or_actor
					modifier = gave_away_de_jure_liege_title_opinion
					opinion = -20
				}
			}
		}
	}
	#Don't want to impose much mental load on giving away counties
	#PLUS you can give them away with the "give to random noble" button without repercussions
	#So we'll only do de jure lieges getting mad you are purposefully favoring another lord or courtier over them
	else_if = {
		limit = {
			$TITLE$ = { tier >= tier_county }
		}
		#Upset they didn't get de jure vassal title
		every_in_list = {
			list = eligible_vassals
			limit = {
				any_held_title = {
					any_in_de_jure_hierarchy = {
						this = $TITLE$
					}
				}
				NOT = {
					any_vassal_or_below = {
						this = scope:recipient
					}
				}
			}
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = gave_away_de_jure_vassal_title_opinion
				opinion = -15
			}
		}
	}
}
strengthen_claim_interaction_effect = {
	scope:puppet_or_actor = { add_pressed_claim = scope:target }
	scope:recipient = { save_scope_as = cleric }
	scope:target.holder ?= { save_scope_as = owner }
	#Let's figure out how morally wrong the cleric feels this is
	#Either your shittiness or target's coolness has given the clergyman pause
	if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 9
		}
		scope:recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = major_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = major_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_high_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_high_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 6
		}
		scope:recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = medium_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = medium_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = major_spiritual_fulfillment_loss
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_high_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_high_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 3
		}
		scope:recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = minor_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = minor_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_high_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_high_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value < 3
			clerical_hesitation_claim_interaction_value >= -2
		}
		scope:recipient = {
			add_piety = {
				value = miniscule_piety_loss
				multiply = {
					value = scope:puppet_or_actor.highest_held_title_tier
					subtract = 1
				}
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value < -2
		}
		scope:recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = miniscule_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= -5
		}
		scope:recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= -8
		}
		scope:recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	else = {
		scope:recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = major_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	scope:target.holder ?= {
		add_opinion = {
			target = scope:puppet_or_actor
			modifier = church_claim_modifier
			opinion = -40
		}
		if = {
			limit = {
				scope:recipient = {
					is_ai = yes
				}
			}
			hidden_effect = {
				add_opinion = {
					target = scope:recipient
					modifier = church_claim_modifier
					opinion = -30
				}
			}
		}
		else = {
			add_opinion = {
				target = scope:recipient
				modifier = church_claim_modifier
				opinion = -30
			}
		}
	}
	#Opinion loss with title's de jure liege - why you trying to steal this when it should be under me?
	scope:target = {
		every_this_title_or_de_jure_above = {
			limit = {
				exists = holder
				NOR = {
					this = scope:target
					holder = {
						OR = {
							this = scope:puppet_or_actor
							this = scope:recipient
							this = scope:target.holder
							#actor's liege being mad at them for contesting other vassals is kinda wrong
							is_liege_or_above_of = scope:puppet_or_actor
						}
					}
				}
			}
			if = {
				limit = {
					any_direct_de_jure_vassal_title = {
						this = scope:target
					}
				}
				holder = { add_to_list = de_jure_direct_lieges }
			}
			else = {
				holder = { add_to_list = de_jure_higher_lieges }
			}
		}
		every_in_list = {
			list = de_jure_direct_lieges
			#More loss for direct liege
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = pressed_church_claim_de_jure_liege_modifier
				opinion = -25
			}
			if = {
				limit = {
					scope:recipient = {
						is_ai = yes
					}
				}
				hidden_effect = {
					add_opinion = {
						target = scope:recipient
						modifier = pressed_church_claim_de_jure_liege_modifier
						opinion = -10
					}
				}
			}
			else = {
				add_opinion = {
					target = scope:recipient
					modifier = pressed_church_claim_de_jure_liege_modifier
					opinion = -10
				}
			}
		}
		every_in_list = {
			list = de_jure_higher_lieges
			limit = {
				NOT = { is_in_list = de_jure_direct_lieges }
			}
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = pressed_church_claim_de_jure_liege_modifier
				opinion = -15
			}
			if = {
				limit = {
					scope:recipient = {
						is_ai = yes
					}
				}
				hidden_effect = {
					add_opinion = {
						target = scope:recipient
						modifier = pressed_church_claim_de_jure_liege_modifier
						opinion = -5
					}
				}
			}
			else = {
				add_opinion = {
					target = scope:recipient
					modifier = pressed_church_claim_de_jure_liege_modifier
					opinion = -5
				}
			}
		}
	}
	if = {
		limit = {
			is_ai = yes
		}
		add_opinion = {
			target = scope:recipient
			modifier = pressed_my_claim
			opinion = {
				value = 10
				if = {
					limit = {
						highest_held_title_tier < scope:target.tier
					}
					multiply = 4
				}
				else_if = {
					limit = {
						highest_held_title_tier = scope:target.tier
					}
					multiply = 3
				}
				else_if = {
					limit = {
						highest_held_title_tier_minus_one = scope:target.tier
					}
					multiply = 2
				}
				else_if = {
					limit = {
						highest_held_title_tier_minus_two = scope:target.tier
					}
					multiply = 1.5
				}
			}
		}
	}
	if = {
		limit = {
			scope:donate_faith = yes
		}
		pay_short_term_gold = {
			target = scope:recipient
			gold = scope:puppet_or_actor.medium_gold_value
		}
	}
	if = {
		limit = {
			scope:use_hook = yes
			has_usable_hook = scope:recipient
		}
		use_hook = scope:recipient
	}
	if = {
		limit = {
			scope:offer_hook = yes
		}
		scope:recipient = {
			add_hook = {
				type = favor_hook
				target = scope:puppet_or_actor
			}
		}
	}
	pam_offer_pilgrimage_effect = {
		ACTOR = scope:puppet_or_actor
		CLERIC = scope:recipient
	}
	if = {
		limit = {
			scope:threaten_priest = yes
		}
		if = {
			limit = {
				scope:secondary_recipient = {
					this = scope:puppet_or_actor.faith.religious_head
				}
			}
			change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
			add_piety_experience = massive_piety_loss
			custom_tooltip = hof_increase_piety_sp_fulfillment_loss_tt
		}
		else = {
			change_spiritual_fulfillment = major_spiritual_fulfillment_loss
			add_piety_experience = major_piety_loss
		}
		scope:secondary_recipient = {
			add_opinion = {
				modifier = threatened_church_opinion
				target = scope:puppet_or_actor
				opinion = -50
			}
		}
	}
	# Catalysts
	if = {
		limit = { # If you are strenghtening a claim against a Christian ruler
			scope:puppet_or_actor.religion = religion:christianity_religion
			scope:target.holder.religion ?= religion:christianity_religion
			situation:the_christian_church ?= { situation_top_has_catalyst = catalyst_the_christian_church_strenghtened_claim }
		}
		situation:the_christian_church = { trigger_situation_catalyst = catalyst_the_christian_church_strenghtened_claim }
	}
}

sell_claim_interaction_effect = {
	scope:recipient = { add_unpressed_claim = scope:target }
	# cost = { piety } is only charged to scope:actor, so a puppeteer's cleric pays here instead
	if = {
		limit = { scope:is_puppet_action ?= yes }
		scope:puppet_or_actor = {
			add_piety = {
				value = {
					add = pam_sell_claim_piety_cost_value
					max = piety
					multiply = -1
				}
			}
		}
	}
	if = {
		limit = { scope:sell_claim_gold_and_hook = yes }
		send_interface_toast = {
			title = sell_claim_interaction_effect_tt
			left_icon = scope:target
			right_icon = scope:recipient
			scope:recipient = {
				pay_short_term_gold = {
					target = scope:puppet_or_actor
					gold = minor_gold_value
				}
			}
		}
		scope:puppet_or_actor = { # Generates its own toast
			if = {
				limit = { has_hook = scope:recipient }
				custom_tooltip = already_has_hook_on_recipient
			}
			add_hook = {
				type = favor_hook
				target = scope:recipient
			}
		}
	}
	else = {
		scope:puppet_or_actor = {
			send_interface_toast = {
				title = sell_claim_interaction_effect_tt
				left_icon = scope:target
				right_icon = scope:recipient
				scope:recipient = {
					pay_short_term_gold = {
						target = scope:puppet_or_actor
						gold = pam_sell_claim_gold_cost_value
					}
				}
			}
		}
	}
	if = {
		limit = {
			scope:is_puppet_action ?= yes
			scope:actor != scope:recipient
		}
		scope:actor = {
			send_interface_toast = {
				title = sell_claim_interaction_effect_tt_puppeteer
				left_icon = scope:puppet_or_actor
				right_icon = scope:recipient
			}
		}
	}
	if = {
		limit = {
			scope:puppet_or_actor = { has_pam_dlc_trigger = yes }
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_clergy_sold_claim
			}
			scope:puppet_or_actor = {
				any_character_situation = {
					this = situation:the_christian_church
				}
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_clergy_sold_claim
		}
	}
}

gift_claim_interaction_effect = {
	send_interface_toast = {
		type = event_toast_effect_neutral
		title = gift_claim_interaction_effect_tt
		left_icon = scope:target
		right_icon = scope:recipient
		remove_claim = scope:target
		scope:recipient = {
			add_unpressed_claim = scope:target
		}
	}
	if = {
		limit = {
			scope:actor != scope:puppet_or_actor
			scope:actor != scope:recipient
		}
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = gift_claim_interaction_effect_tt_puppeteer
				left_icon = scope:puppet_or_actor
				right_icon = scope:recipient
			}
		}
	}
}

ecclesiastic_grant_claim_interaction_effect = {
	add_pressed_claim = scope:target
	scope:secondary_recipient = { save_scope_as = cleric }
	scope:recipient = { save_scope_as = owner }
	#Either your shittiness or target's coolness has given the clergyman pause
	if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 9
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = major_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = major_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_high_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_high_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 6
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = medium_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = medium_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = major_spiritual_fulfillment_loss
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_high_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_high_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 3
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = minor_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = minor_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_high_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_high_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value < 3
			clerical_hesitation_claim_interaction_value >= -2
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value < -2
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = miniscule_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= -5
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= -8
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	else = {
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:puppet_or_actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:puppet_or_actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = major_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	scope:recipient ?= {
		add_opinion = {
			target = scope:puppet_or_actor
			modifier = claimed_my_title_opinion
			opinion = -60
		}
		progress_towards_rival_effect = {
			CHARACTER = scope:puppet_or_actor
			OPINION = 0
			REASON = rival_claimed_my_title
		}
		hidden_effect = {
			add_opinion = {
				target = scope:secondary_recipient
				modifier = claimed_my_title_opinion
				opinion = -35
			}
		}
	}
	#Opinion loss with title's de jure liege - why you trying to steal this when it should be under me?
	scope:target = {
		every_this_title_or_de_jure_above = {
			limit = {
				exists = holder
				NOR = {
					this = scope:target
					holder = {
						OR = {
							this = scope:puppet_or_actor
							this = scope:recipient
							this = scope:target.holder
							#actor's liege being mad at them for contesting other vassals is kinda wrong
							is_liege_or_above_of = scope:puppet_or_actor
						}
					}
				}
			}
			if = {
				limit = {
					any_direct_de_jure_vassal_title = {
						this = scope:target
					}
				}
				holder = { add_to_list = de_jure_direct_lieges }
			}
			else = {
				holder = { add_to_list = de_jure_higher_lieges }
			}
		}
		every_in_list = {
			list = de_jure_direct_lieges
			#More loss for direct liege
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = church_claim_de_jure_liege_modifier
				opinion = -25
			}
			if = {
				limit = {
					scope:secondary_recipient = { is_ai = yes }
				}
				hidden_effect = {
					add_opinion = {
						target = scope:secondary_recipient
						modifier = church_claim_de_jure_liege_modifier
						opinion = -10
					}
				}
			}
			else = {
				add_opinion = {
					target = scope:secondary_recipient
					modifier = church_claim_de_jure_liege_modifier
					opinion = -10
				}
			}
		}
		every_in_list = {
			list = de_jure_higher_lieges
			limit = {
				NOT = { is_in_list = de_jure_direct_lieges }
			}
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = church_claim_de_jure_liege_modifier
				opinion = -15
			}
			if = {
				limit = {
					scope:secondary_recipient = { is_ai = yes }
				}
				hidden_effect = {
					add_opinion = {
						target = scope:secondary_recipient
						modifier = church_claim_de_jure_liege_modifier
						opinion = -5
					}
				}
			}
			else = {
				add_opinion = {
					target = scope:secondary_recipient
					modifier = church_claim_de_jure_liege_modifier
					opinion = -5
				}
			}
		}
	}
	if = {
		limit = {
			is_ai = yes
		}
		add_opinion = {
			target = scope:secondary_recipient
			modifier = granted_claim_opinion
			opinion = {
				value = 10
				if = {
					limit = {
						highest_held_title_tier < scope:target.tier
					}
					multiply = 5
				}
				else_if = {
					limit = {
						highest_held_title_tier = scope:target.tier
					}
					multiply = 4
				}
				else_if = {
					limit = {
						highest_held_title_tier_minus_one = scope:target.tier
					}
					multiply = 3
				}
				else_if = {
					limit = {
						highest_held_title_tier_minus_two = scope:target.tier
					}
					multiply = 2
				}
			}
		}
	}
	if = {
		limit = {
			scope:donate_faith = yes
		}
		pay_short_term_gold = {
			target = scope:secondary_recipient
			gold = scope:puppet_or_actor.medium_gold_value
		}
	}
	if = {
		limit = {
			scope:use_hook = yes
			has_usable_hook = scope:secondary_recipient
		}
		use_hook = scope:secondary_recipient
	}
	if = {
		limit = {
			scope:offer_hook = yes
		}
		scope:secondary_recipient = {
			add_hook = {
				type = favor_hook
				target = scope:puppet_or_actor
			}
		}
	}
	pam_offer_pilgrimage_effect = {
		ACTOR = scope:puppet_or_actor
		CLERIC = scope:secondary_recipient
	}
	if = {
		limit = {
			scope:threaten_priest = yes
		}
		if = {
			limit = {
				scope:secondary_recipient = {
					this = scope:puppet_or_actor.faith.religious_head
				}
			}
			change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
			add_piety_experience = massive_piety_loss
			custom_tooltip = hof_increase_piety_sp_fulfillment_loss_secondary_tt
		}
		else = {
			change_spiritual_fulfillment = major_spiritual_fulfillment_loss
			add_piety_experience = major_piety_loss
		}
		scope:secondary_recipient = {
			add_opinion = {
				modifier = threatened_church_opinion
				target = scope:puppet_or_actor
				opinion = -50
			}
		}
	}
	# Catalysts
	if = {
		limit = {
			scope:puppet_or_actor.religion = religion:christianity_religion
			scope:recipient.religion = religion:christianity_religion
			situation:the_christian_church ?= { situation_top_has_catalyst = catalyst_the_christian_church_ecclesiastical_grant_claim }
		}
		if = {
			limit = { scope:threaten_priest = yes }
			situation:the_christian_church = { trigger_situation_catalyst = catalyst_the_christian_church_threatened_ecclesiastical_grant_claim }
		}
		else = {
			situation:the_christian_church = { trigger_situation_catalyst = catalyst_the_christian_church_ecclesiastical_grant_claim }
		}
	}
}

# Called from on_personal_tenet_gain and on_piety_level_gain
reincarnation_piety_courage_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_reincarnation_piety_level_courage
			piety_level >= high_piety_level
		}
		if = {
			limit = { has_trait = craven }
			send_interface_toast = {
				type = event_toast_effect_good
				title = pam_reincarnation_courage_toast
				left_icon = root
				remove_trait = craven
			}
		}
		else_if = {
			limit = { NOT = { has_trait = brave } }
			send_interface_toast = {
				type = event_toast_effect_good
				title = pam_reincarnation_courage_toast
				left_icon = root
				add_trait = brave
			}
		}
	}
}

exaltation_of_pain_flagellant_gain_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_exaltation_of_pain_flagellant_trait
			NOT = { has_trait = flagellant }
		}
		add_trait = flagellant
	}
}

pam_kingdom_of_heaven_declaration_effect = {
	save_scope_as = kingdom_of_heaven_founder
	get_title = title:h_kingdom_of_heaven
	set_primary_title_to = title:h_kingdom_of_heaven
	set_realm_capital = title:c_jerusalem
	hidden_effect = {
		domicile ?= { move_domicile = root.capital_province }
	}
	dynasty ?= {
		add_dynasty_prestige = 10000
		add_dynasty_modifier = pam_founded_kingdom_of_heaven_modifier
	}
	if = {
		limit = { NOT = { exists = dynasty } }
		add_character_modifier = pam_founded_kingdom_of_heaven_modifier
	}
	custom_tooltip = {
		text = pam_kingdom_of_heaven_decision_subjugation_effect_tt
		set_variable = koh_subjugation_cb_allowed
	}
	set_global_variable = {
		name = created_kingdom_of_heaven
		value = yes
	}
	root.faith = {
		every_faith_ruler = {
			limit = {
				this != root
				is_independent_ruler = yes
				is_available_quick = {
					landed = yes
					ai = yes
					advanced_ruler = yes
				}
				is_playable_character = yes
				highest_held_title_tier >= tier_county
				highest_held_title_tier < tier_hegemony
				NOR = {
					has_relation_rival = root
					is_at_war_with = root
					government_has_flag = cannot_be_vassal_or_liege
				}
			}
			random = {
				chance = {
					value = 20
					if = {
						limit = { piety_level >= 1 }
						add = 5
					}
					if = {
						limit = { piety_level >= 2 }
						add = 8
					}
					if = {
						limit = { piety_level >= 3 }
						add = 10
					}
					if = {
						limit = { piety_level >= 4 }
						add = 15
					}
					if = {
						limit = {
							opinion = {
								target = root
								value >= 30
							}
						}
						add = 10
					}
					if = {
						limit = {
							opinion = {
								target = root
								value >= 60
							}
						}
						add = 15
					}
					if = {
						limit = {
							exists = rite
							scope:kingdom_of_heaven_founder = { exists = rite }
							rite = scope:kingdom_of_heaven_founder.rite
						}
						add = 40
					}
				}
				add_to_list = new_vassals
				hidden_effect = {
					create_title_and_vassal_change = {
						type = swear_fealty
						save_scope_as = koh_fealty_change
						add_claim_on_loss = no
					}
					change_liege = {
						liege = root
						change = scope:koh_fealty_change
					}
					resolve_title_and_vassal_change = scope:koh_fealty_change
				}
				if = {
					limit = { highest_held_title_tier >= tier_kingdom }
					show_as_tooltip = {
						change_liege = {
							liege = root
							change = scope:koh_fealty_change
						}
					}
				}
			}
		}
	}
	custom_tooltip = kingdom_of_heaven_lower_vassals_gained_tt
	custom_tooltip = {
		text = kingdom_of_heaven_lower_de_jure_gained_tt
		every_empire = {
			limit = {
				is_landless_type_title = no
				trigger_if = {
					limit = { exists = holder.top_liege }
					holder.top_liege = root
				}
				# really not landless
				any_in_de_jure_hierarchy = {
					tier = tier_kingdom
					is_landless_type_title = no
					any_in_de_jure_hierarchy = {
						tier = tier_duchy
						is_landless_type_title = no
					}
				}
				save_temporary_scope_as = empire_temp
				OR = {
					holder ?= scope:kingdom_of_heaven_founder
					scope:kingdom_of_heaven_founder = { completely_controls = scope:empire_temp }
				}
			}
			set_de_jure_liege_title = title:h_kingdom_of_heaven
		}
	}
}

pam_create_theocratic_dynasty_extra_effects = {
	if = { # HoF doesn't like this
		limit = {
			exists = faith.religious_head
			root != faith.religious_head
			faith = { has_doctrine = doctrine_clerical_marriage_disallowed }
		}
		faith.religious_head = {
			add_opinion = {
				target = root
				modifier = impious_opinion
				opinion = -40
			}
		}
		add_piety = major_piety_loss
		situation:the_christian_church ?= {
			if = {
				limit = {
					situation_top_has_catalyst = catalyst_the_christian_church_established_theocratic_dynasty
				}
				trigger_situation_catalyst = catalyst_the_christian_church_established_theocratic_dynasty
			}
		}
	}
}

pam_boost_clerical_appointment_score_effect = {
	$CHARACTER$.superior.primary_title ?= {
		change_appointment_investment = {
			target = $CHARACTER$
			value = $VALUE$
		}
	}
}

# Successful Siphon Treasury
pam_the_christian_church_clergy_embezzled_treasury_catalyst_effect = {
	if = {
		limit = {
			is_clergy = yes
			any_character_situation = {
				situation_type = the_christian_church
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_clergy_embezzled_treasury
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = {
				catalyst = catalyst_clergy_embezzled_treasury
				character = root
			}
		}
	}
}

# Withdraw Funds from Treasury decision
pam_the_christian_church_clergy_withdrew_funds_from_treasury_catalyst_effect = {
	if = {
		limit = {
			is_clergy = yes
			any_character_situation = {
				situation_type = the_christian_church
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_clergy_withdrew_funds_from_treasury
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_clergy_withdrew_funds_from_treasury
		}
	}
}

# Needs POPE (appointer), CARDINAL (new cardinal) and OTHERWISE (non-nepotistic catalyst), any scope
# Both branches guard their catalyst, so a phase listing neither books nothing
pam_the_christian_church_appointed_cardinal_catalyst_effect = {
	situation:the_christian_church ?= {
		if = {
			limit = {
				$CARDINAL$ = { pam_cardinal_is_nepotistic_appointment_trigger = { POPE = $POPE$ } }
			}
			if = {
				limit = {
					situation_top_has_catalyst = catalyst_the_christian_church_appointed_nepotistic_cardinal
				}
				trigger_situation_catalyst = catalyst_the_christian_church_appointed_nepotistic_cardinal
			}
		}
		else = {
			if = {
				limit = {
					situation_top_has_catalyst = $OTHERWISE$
				}
				trigger_situation_catalyst = $OTHERWISE$
			}
		}
	}
}

# A non-Catholic ruler holds the de jure capital county of k_papal_state
pam_the_christian_church_catholic_hof_weak_papal_state_control_catalyst_effect = {
	if = {
		limit = {
			has_pam_dlc_trigger = yes
			pam_catholic_hof_weak_papal_state_control_trigger = yes
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_catholic_hof_weak_papal_state_control
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_catholic_hof_weak_papal_state_control
		}
	}
}

@pam_papal_dynasty_capture_reigns = 3
pam_the_christian_church_papal_dynasty_capture_catalyst_effect = {
	if = {
		limit = {
			has_pam_dlc_trigger = yes
			current_date_is_start_date_trigger = no
			religion = religion:christianity_religion
		}
		if = {
			limit = { exists = dynasty }
			dynasty = { save_scope_as = pam_new_papal_dynasty }
		}
		situation:the_christian_church ?= {
			if = {
				limit = {
					exists = scope:pam_new_papal_dynasty
					has_variable = pam_papal_dynasty
					var:pam_papal_dynasty = scope:pam_new_papal_dynasty
				}
				change_variable = {
					name = pam_papal_dynasty_reigns
					add = 1
				}
			}
			else_if = {
				limit = { exists = scope:pam_new_papal_dynasty }
				set_variable = {
					name = pam_papal_dynasty
					value = scope:pam_new_papal_dynasty
				}
				set_variable = {
					name = pam_papal_dynasty_reigns
					value = 1
				}
				remove_variable = pam_papal_dynasty_capture_catalyst_fired
			}
			else = {
				remove_variable = pam_papal_dynasty
				remove_variable = pam_papal_dynasty_reigns
				remove_variable = pam_papal_dynasty_capture_catalyst_fired
			}
			if = {
				limit = {
					has_variable = pam_papal_dynasty_reigns
					var:pam_papal_dynasty_reigns >= @pam_papal_dynasty_capture_reigns
					NOT = { has_variable = pam_papal_dynasty_capture_catalyst_fired }
					situation_top_has_catalyst = catalyst_the_christian_church_papal_dynasty_capture
				}
				set_variable = pam_papal_dynasty_capture_catalyst_fired
				trigger_situation_catalyst = catalyst_the_christian_church_papal_dynasty_capture
			}
		}
	}
}

# A different Christian faith becomes Main Power
pam_the_christian_church_main_power_faith_changed_catalyst_effect = {
	situation:the_christian_church ?= {
		if = {
			limit = {
				has_variable = pam_main_power_faith_changed_catalyst_faith
				NOT = { var:tcc_main_power_faith = var:pam_main_power_faith_changed_catalyst_faith }
			}
			remove_variable = pam_the_christian_church_main_power_faith_changed_catalyst_fired
			remove_variable = pam_main_power_faith_changed_catalyst_faith
		}
		if = {
			limit = {
				exists = scope:tcc_old_main_power_faith
				has_variable = tcc_main_power_faith
				var:tcc_main_power_faith != scope:tcc_old_main_power_faith
				NOT = { has_variable = pam_the_christian_church_main_power_faith_changed_catalyst_fired }
				situation_top_has_catalyst = catalyst_the_christian_church_main_power_faith_changed
			}
			set_variable = pam_the_christian_church_main_power_faith_changed_catalyst_fired
			set_variable = {
				name = pam_main_power_faith_changed_catalyst_faith
				value = var:tcc_main_power_faith
			}
			trigger_situation_catalyst = catalyst_the_christian_church_main_power_faith_changed
		}
	}
}

# Byzantine emperor is not Christian
pam_the_christian_church_byzantine_emperor_non_christian_catalyst_effect = {
	situation:the_christian_church ?= {
		if = {
			limit = {
				pam_byzantine_emperor_non_christian_trigger = no
			}
			remove_variable = pam_byzantine_emperor_non_christian_catalyst_fired
		}
		else_if = {
			limit = {
				pam_byzantine_emperor_non_christian_trigger = yes
				NOT = { has_variable = pam_byzantine_emperor_non_christian_catalyst_fired }
				situation_top_has_catalyst = catalyst_byzantine_emperor_non_christian
			}
			set_variable = pam_byzantine_emperor_non_christian_catalyst_fired
			trigger_situation_catalyst = catalyst_byzantine_emperor_non_christian
		}
	}
}

# Non Christian presence in Europe
pam_the_christian_church_non_christian_kingdom_in_europe_catalyst_effect = {
	if = {
		limit = {
			pam_non_christian_kingdom_holder_in_europe_trigger = yes
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_non_christian_kingdom_in_europe
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_non_christian_kingdom_in_europe
		}
		random_ruler = {
			limit = { pam_non_christian_kingdom_holder_in_europe_trigger = yes }
			set_variable = pam_non_christian_kingdom_holder_in_europe_trigger_var
		}
	}
}

# County conversion Catalysts
pam_the_christian_church_county_conversion_catalysts_effect = {
	if = { # PAM - Catalysts if non-Christian county is converted to Christianity
		limit = {
			faith != scope:old_faith
			scope:old_faith = {
				religion != religion:christianity_religion
			}
			faith.religion = religion:christianity_religion
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_county_converted_to_faith
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_county_converted_to_faith
		}
	}
	if = { # PAM - Catalyst when Christian county is converted away from Christianity
		limit = {
			faith != scope:old_faith
			scope:old_faith = {
				religion = religion:christianity_religion
			}
			faith.religion != religion:christianity_religion
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_county_converted_from_faith
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_county_converted_from_faith
		}
	}
	if = { # PAM - Jerusalem county is no longer Christian
		limit = {
			this = title:c_jerusalem
			scope:old_faith = {
				religion = religion:christianity_religion
			}
			faith.religion != religion:christianity_religion
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_lose_jerusalem
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_lose_jerusalem
		}
	}
	if = { # PAM - Other Christian holy site county is no longer Christian
		limit = {
			NOT = { this = title:c_jerusalem }
			scope:old_faith = {
				religion = religion:christianity_religion
			}
			faith.religion != religion:christianity_religion
			any_county_province = {
				barony = { is_holy_site_of = scope:old_faith }
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_lose_holy_site
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_lose_holy_site
		}
	}
}

# Clergy member exposed as adhering to a prohibited personal tenet
pam_the_christian_church_clergy_exposed_forbidden_personal_tenet_catalyst_effect = {
	if = {
		limit = {
			is_clergy = yes
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_clergy_exposed_forbidden_personal_tenet
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_clergy_exposed_forbidden_personal_tenet
		}
	}
}

# Secular ruler exposed as adhering to a prohibited personal tenet
pam_the_christian_church_exposed_forbidden_personal_tenet_catalyst_effect = {
	if = {
		limit = {
			is_clergy = no
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_exposed_forbidden_personal_tenet
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_exposed_forbidden_personal_tenet
		}
	}
}

# Christian ruler wins a Great Holy War
pam_the_christian_church_great_holy_war_won_catalyst_effect = {
	if = {
		limit = {
			scope:attacker = {
				has_pam_dlc_trigger = yes
				religion = religion:christianity_religion
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_the_christian_church_great_holy_war_won
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_the_christian_church_great_holy_war_won
		}
	}
}

# Christian ruler loses a Great Holy War
pam_the_christian_church_great_holy_war_lost_catalyst_effect = {
	if = {
		limit = {
			scope:attacker = {
				has_pam_dlc_trigger = yes
				religion = religion:christianity_religion
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_the_christian_church_great_holy_war_lost
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_the_christian_church_great_holy_war_lost
		}
	}
}

# Christian prelate hires a Military Order (on_holy_order_hired)
pam_the_christian_church_clergy_hired_holy_order_catalyst_effect = {
	if = {
		limit = {
			scope:actor = {
				has_pam_dlc_trigger = yes
				is_clergy = yes
				religion = religion:christianity_religion
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_clergy_hired_holy_order
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_clergy_hired_holy_order
		}
	}
}

# Christian prelate hires mercenaries while at war with a non-Christian (on_mercenary_company_hired)
pam_the_christian_church_clergy_hired_mercenaries_catalyst_effect = {
	if = {
		limit = {
			scope:employer = {
				has_pam_dlc_trigger = yes
				is_clergy = yes
				religion = religion:christianity_religion
				any_primary_war_enemy = {
					religion != religion:christianity_religion
				}
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_clergy_hired_mercenaries
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_clergy_hired_mercenaries
		}
	}
}

pam_the_christian_church_clergy_gained_landed_title_catalyst_effect = {
	if = {
		limit = {
			has_global_variable = game_has_started  # Don't fire for the clerical title holders placed during setup
			has_pam_dlc_trigger = yes
			is_clergy = yes
			religion = religion:christianity_religion
			scope:title = {
				tier >= tier_county
				has_clerical_region = no
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_clergy_gained_landed_title
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = {
				catalyst = catalyst_clergy_gained_landed_title
				character = root
			}
		}
	}
}

# Excommunication war declared between two Christian rulers (excommunication_war on_declaration)
pam_the_christian_church_excommunication_war_catalyst_effect = {
	if = {
		limit = {
			scope:attacker = { has_pam_dlc_trigger = yes }
			scope:attacker = { religion = religion:christianity_religion }
			scope:defender = { religion = religion:christianity_religion }
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_the_christian_church_excommunication_war_declared
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_the_christian_church_excommunication_war_declared
		}
	}
}

# Humiliation war declared against a Christian ecclesiastical ruler
pam_the_christian_church_humiliation_war_catalyst_effect = {
	if = {
		limit = {
			scope:attacker = { has_pam_dlc_trigger = yes }
			scope:defender = {
				religion = religion:christianity_religion
				government_has_flag = government_is_theocracy # Accounts for Ecclesiastical
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_the_christian_church_humiliation_war_declared
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_the_christian_church_humiliation_war_declared
		}
	}
}

# Clergy has a child
pam_the_christian_church_clergy_has_child_catalyst_effect = {
	$CHARACTER$ = {
		if = {
			limit = {
				has_pam_dlc_trigger = yes
				is_clergy = yes
				rite = { rite_has_doctrine = doctrine_clerical_marriage_disallowed }
				any_character_situation = {
					situation_type = the_christian_church
					situation_top_has_catalyst = catalyst_clergy_has_child
				}
			}
			if = {
				limit = {
					any_held_title = { is_head_of_faith = yes }
				}
				situation:the_christian_church = {
					trigger_situation_catalyst = {
						catalyst = catalyst_the_christian_church_head_of_faith_has_child
						character = $CHARACTER$
					}
				}
			}
			else = {
				situation:the_christian_church = {
					trigger_situation_catalyst = {
						catalyst = catalyst_clergy_has_child
						character = $CHARACTER$
					}
				}
			}
		}
	}
}

pam_buy_ecclesiastical_title_interaction_effect = {
	scope:actor = {
		if = {
			limit = { government_has_flag = government_is_theocracy }
			save_scope_as = new_holder
		}
		else = {
			scope:secondary_recipient = { save_scope_as = new_holder }
		}
	}
	scope:actor = {
		pay_short_term_gold = {
			target = scope:recipient
			gold = pam_buy_ecclesiastical_title_gold_cost_value
		}
	}
	create_title_and_vassal_change = {
		type = granted
		save_scope_as = change
	}
	scope:target = {
		change_title_holder_include_vassals = {
			holder = scope:new_holder
			change = scope:change
			government_base = scope:recipient
		}
	}
	resolve_title_and_vassal_change = scope:change
	scope:new_holder = {
		if = {
			limit = {
				NOT = { government_has_flag = government_is_ecclesiastical }
			}
			change_government = ecclesiastical_government
		}
	}
	if = {
		limit = {
			scope:actor = { has_pam_dlc_trigger = yes }
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_clergy_simony
			}
			scope:actor = {
				any_character_situation = {
					this = situation:the_christian_church
				}
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_clergy_simony
		}
	}
}

ecclesiastic_transfer_land_interaction_immediate_effect = {
	scope:secondary_recipient = { save_scope_as = cleric }
	scope:recipient = { save_scope_as = owner }
	#Either your shittiness or target's coolness has given the clergyman pause
	if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 9
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = major_piety_loss
						multiply = {
							value = scope:actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = major_piety_loss
					multiply = {
						value = scope:actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_high_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_high_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 6
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = medium_piety_loss
						multiply = {
							value = scope:actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = medium_piety_loss
					multiply = {
						value = scope:actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = major_spiritual_fulfillment_loss
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_high_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_high_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 3
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = minor_piety_loss
						multiply = {
							value = scope:actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = minor_piety_loss
					multiply = {
						value = scope:actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_high_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_high_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value < 3
			clerical_hesitation_claim_interaction_value >= -2
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value < -2
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = miniscule_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= -5
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= -8
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = medium_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	else = {
		scope:secondary_recipient = {
			if = {
				limit = {
					is_ai = yes
					piety <= 0
				}
				show_as_tooltip = {
					add_piety = {
						value = miniscule_piety_loss
						multiply = {
							value = scope:actor.highest_held_title_tier
							subtract = 1
						}
					}
				}
			}
			else = {
				add_piety = {
					value = miniscule_piety_loss
					multiply = {
						value = scope:actor.highest_held_title_tier
						subtract = 1
					}
				}
			}
			change_spiritual_fulfillment = major_spiritual_fulfillment_value
			if = {
				limit = {
					root ?= scope:cleric
				}
				custom_tooltip = clerical_hesitation_low_player_tt
			}
			else = {
				custom_tooltip = clerical_hesitation_low_tt
			}
		}
	}
	#Opinion effects
	scope:target.holder ?= {
		#Hide this info from the cleric secondary_recipient
		if = {
			limit = {
				root ?= scope:secondary_recipient
			}
			hidden_effect = {
				add_opinion = {
					target = scope:actor
					modifier = demanded_my_title_opinion
					opinion = -50
				}
			}
		}
		else = {
			add_opinion = {
				target = scope:actor
				modifier = demanded_my_title_opinion
				opinion = -50
			}
		}
		if = {
			limit = {
				scope:secondary_recipient = {
					is_ai = yes
				}
			}
			hidden_effect = {
				add_opinion = {
					target = scope:secondary_recipient
					modifier = demanded_my_title_opinion
					opinion = -30
				}
			}
		}
	}
	if = {
		limit = {
			scope:recipient = {
				NOT = {
					this = scope:target.holder
				}
			}
		}
		scope:recipient ?= {
			#Hide this info from the cleric secondary_recipient
			if = {
				limit = {
					root ?= scope:secondary_recipient
				}
				hidden_effect = {
					add_opinion = {
						target = scope:actor
						modifier = demanded_my_title_opinion
						opinion = -25
					}
				}
			}
			else = {
				add_opinion = {
					target = scope:actor
					modifier = demanded_my_title_opinion
					opinion = -25
				}
			}


			if = {
				limit = {
					scope:secondary_recipient = {
						is_ai = yes
					}
				}
				hidden_effect = {
					add_opinion = {
						target = scope:secondary_recipient
						modifier = demanded_my_title_opinion
						opinion = -20
					}
				}
			}
		}
	}
	if = {
		limit = {
			is_ai = yes
		}
		add_opinion = {
			target = scope:secondary_recipient
			modifier = clerical_transfer_title_opinion
			opinion = {
				value = 10
				if = {
					limit = {
						highest_held_title_tier < scope:target.tier
					}
					multiply = 8
				}
				else_if = {
					limit = {
						highest_held_title_tier = scope:target.tier
					}
					multiply = 5
				}
				else_if = {
					limit = {
						highest_held_title_tier_minus_one = scope:target.tier
					}
					multiply = 3
				}
				else_if = {
					limit = {
						highest_held_title_tier_minus_two = scope:target.tier
					}
					multiply = 2
				}
			}
		}
	}
	#Send toast to actor to confirm that interaction option costs have been applied
	if = {
		limit = {
			OR = {
				scope:promise_church_succession = yes
				scope:donate_faith = yes
				AND = {
					scope:use_hook = yes
					has_usable_hook = scope:secondary_recipient
				}
				scope:offer_pilgrimage = yes
				scope:threaten_priest = yes
			}
		}
		trigger_event = pam_interactions_events.0038
		#You promised the church could inherit this title if it is ever without heirs
		if = {
			limit = {
				scope:promise_church_succession = yes
			}
			scope:actor = {
				set_fallback_succession_title = scope:secondary_recipient.primary_title
			}
			scope:actor = {
				custom_tooltip = church_fallback_succession_promise_tt
			}
		}
		if = {
			limit = {
				scope:donate_faith = yes
			}
			pay_short_term_gold = {
				target = scope:secondary_recipient
				gold = scope:actor.medium_gold_value
			}
		}
		if = {
			limit = {
				scope:use_hook = yes
				has_usable_hook = scope:secondary_recipient
			}
			use_hook = scope:secondary_recipient
		}
		if = {
			limit = {
				scope:offer_hook = yes
			}
			scope:secondary_recipient = {
				add_hook = {
					type = strong_obligation_hook
					target = scope:actor
				}
			}
		}
		pam_offer_pilgrimage_effect = {
			ACTOR = scope:actor
			CLERIC = scope:secondary_recipient
		}
		if = {
			limit = {
				scope:threaten_priest = yes
			}
			if = {
				limit = {
					scope:secondary_recipient = {
						this = scope:actor.faith.religious_head
					}
				}
				change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
				add_piety_experience = massive_piety_loss
				custom_tooltip = hof_increase_piety_sp_fulfillment_loss_secondary_tt
			}
			else = {
				change_spiritual_fulfillment = major_spiritual_fulfillment_loss
				add_piety_experience = major_piety_loss
			}
			scope:secondary_recipient = {
				add_opinion = {
					modifier = threatened_church_opinion
					target = scope:actor
					opinion = -50
				}
			}
		}
	}
	#Catalyst towards saeculum_obscurum
	if = {
		limit = {
			#Only fire catalyst if clergyman is choosing a more immoral holder (has decently high hesitation)
			clerical_hesitation_claim_interaction_value >= -2
			any_character_situation = {
				this = situation:the_christian_church
			}
			situation:the_christian_church = {
				OR = {
					situation_top_has_catalyst = catalyst_ecclesiastical_transfer_land_interaction
					situation_top_has_catalyst = catalyst_ecclesiastical_transfer_land_interaction_major
				}
			}
		}
		situation:the_christian_church = {
			if = {
				limit = {
					scope:target.tier >= tier_kingdom
				}
				trigger_situation_catalyst = catalyst_ecclesiastical_transfer_land_interaction_major
			}
			else = {
				trigger_situation_catalyst = catalyst_ecclesiastical_transfer_land_interaction
			}
		}

	}
}

ecclesiastic_transfer_land_interaction_transfer_effect = {
	#Transfer vassal
	#Need to inform player of why they were just transferred
	#Players are only valid if it is their primary title being transfered
	if = {
		limit = {
			scope:target.holder ?= {
				NOT = {
					this = scope:recipient
				}
				is_ai = no
			}
		}
		#transfer vassal
		if = {
			limit = {
				scope:target.holder = { is_governor_or_admin_count = no }
			}
			scope:target.holder = {
				add_to_list = already_transfered
				every_vassal_or_below = {
					limit = {
						highest_held_title_tier >= tier_county
					}
					add_to_list = already_transfered
				}
			}
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = no
			}
			scope:target.holder = {
				change_liege = {
					liege = scope:actor
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change

			scope:target.holder = {
				#Notification event they're being transferred
				trigger_event = pam_interactions_events.0037
			}
		}
		#Admin - needs title revoked
		else = {
			every_held_title = {
				limit = {
					any_this_title_or_de_jure_above = {
						this = scope:target
					}
				}
				add_to_list = admin_target_transfer
			}
			hidden_effect = {
				create_title_and_vassal_change = {
					type = revoked
					save_scope_as = change
					add_claim_on_loss = yes
				}
				every_in_list = {
					list = admin_target_transfer
					change_title_holder = {
						holder = scope:recipient
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
			scope:target.holder = {
				#Notification event to dispossessed admin
				trigger_event = pam_interactions_events.0039
			}
		}
	}
	#Transfer vassal if it is primary title of its holder
	else_if = {
		limit = {
			scope:target.holder.primary_title ?= scope:target
			scope:target.holder = {
				highest_held_title_tier = scope:target.tier
				is_governor_or_admin_count = no
			}
			OR = {
				#already low enough tier to transfer
				scope:target.holder.highest_held_title_tier < scope:actor.highest_held_title_tier
				#Will be low enough tier to transfer
				scope:target.holder = {
					NOT = {
						any_held_title = {
							NOT = {
								this = scope:target
							}
							tier = scope:target.tier
						}
					}
				}
			}
		}
		scope:target.holder = { save_scope_as = old_holder }
		#Need to transfer vassals if getting taken down a tier
		if = {
			limit = {
				#Will be low enough tier to transfer
				scope:target.holder = {
					NOT = {
						any_held_title = {
							NOT = {
								this = scope:target
							}
							tier >= scope:target.tier
						}
					}
				}
			}
			scope:old_holder = {
				every_vassal = {
					limit = {
						highest_held_title_tier >= tier_county
						primary_title = {
							any_this_title_or_de_jure_above = {
								this = scope:target
							}
						}
						highest_held_title_tier >= scope:target.tier_minus_one
						is_governor_or_admin_count = no
					}
					add_to_list = transfer_vassals
				}
			}
		}
		#Transferring title still if needed to vassalize recipient
		if = {
			limit = {
				scope:target.tier >= scope:actor.highest_held_title_tier
			}
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = yes
			}
			scope:target = {
				change_title_holder = {
					holder = scope:actor
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
		scope:old_holder = {
			add_to_list = already_transfered
			every_vassal_or_below = {
				limit = {

				}
				add_to_list = already_transfered
			}
		}
		custom_tooltip = clergy_transfer_vassal_tt
		hidden_effect = {
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = no
			}
			scope:old_holder = {
				change_liege = {
					liege = scope:actor
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
	#transfer the title without the holder
	else = {
		scope:target ?= {
			add_to_list = titles_to_grant
		}
	}
	scope:target ?= {
		#Revoke titles from admins in the transfer zone
		every_in_de_jure_hierarchy = {
			limit = {
				tier >= tier_county
				holder ?= {
					is_vassal_or_below_of = scope:recipient
					is_governor_or_admin_count = yes
				}
				NOT = { is_in_list = titles_to_grant }
			}
			add_to_list = admin_title_losses
		}
		hidden_effect = {
			create_title_and_vassal_change = {
				type = revoked
				save_scope_as = change
				add_claim_on_loss = yes
			}
			every_in_list = {
				list = admin_title_losses
				change_title_holder = {
					holder = scope:recipient
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}

		#Transfer vassals
		every_in_de_jure_hierarchy = {
			limit = {
				exists = holder
				holder = {
					is_governor_or_admin_count = no
				}
			}
			#Transfer vassals/sub-vassals of recipient that belong under target title
			#At this point, target holder may be transfered and thus their vassals would not be included in this
			if = {
				limit = {
					holder = {
						highest_held_title_tier >= tier_county
						any_liege_or_above = {
							this = scope:recipient
						}
						primary_title = {
							any_this_title_or_de_jure_above = {
								this = scope:target
							}
						}
						#Liege is also being transferred
						NOT = {
							any_liege_or_above = {
								NOT = {
									this = scope:target.holder
								}
								primary_title = {
									any_this_title_or_de_jure_above = {
										this = scope:target
									}
								}
							}
						}
						NOR = {
							is_in_list = transfer_vassals
							is_in_list = already_transfered
						}
						highest_held_title_tier < scope:actor.highest_held_title_tier
					}
				}
				holder = {	add_to_list = transfer_vassals }
			}
		}
	}
	#transfer title(s)
	if = {
		limit = {
			any_in_list = {
				list = titles_to_grant
				count >= 1
			}
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = titles_to_grant
			limit = {
				tier >= tier_county
			}
			change_title_holder = {
				holder = scope:actor
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
	}
	#transfer vassals
	if = {
		limit = {
			any_in_list = {
				list = transfer_vassals
				count >= 1
				any_held_title = {
					is_landless_type_title = no
					NOT = {
						is_in_list = titles_to_grant
					}
				}
			}
		}
		if = {
			limit = {
				any_in_list = {
					list = transfer_vassals
					this = scope:target.holder
				}
			}
			custom_tooltip = clergy_transfer_vassal_tt
		}
		hidden_effect = {
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = yes
			}
			every_in_list = {
				list = transfer_vassals
				change_liege = {
					liege = scope:actor
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
	#Make sure recipient isn't sneakily holding onto titles beneath the target title
	if = {
		limit = {
			scope:recipient = {
				NOT = {
					is_vassal_of = scope:actor
				}
			}
		}
		scope:recipient = {
			every_held_title = {
				limit = {
					tier >= tier_county
					any_this_title_or_de_jure_above = {
						this = scope:target
					}
				}
				add_to_list = recipient_mop_up
			}
		}
		hidden_effect = {
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = yes
			}
			every_in_list = {
				list = recipient_mop_up
				limit = {
					tier >= tier_county
				}
				change_title_holder = {
					holder = scope:actor
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
	if = {
		limit = {
			scope:target.tier >= highest_held_title_tier_minus_one
		}
		create_character_memory = {
			type = negotiated_for_land_memory
			participants = {
				recipient = scope:recipient
			}
		}
		scope:new_memory = {
			set_variable = {
				name = target_title
				value = scope:target
			}
		}
	}
	if = {
		limit = {
			scope:target.tier >= scope:recipient.highest_held_title_tier_minus_one
		}
		scope:recipient = {
			create_character_memory = {
				type = gave_away_land_memory
				participants = {
					actor = scope:actor
				}
			}
			scope:new_memory = {
				set_variable = {
					name = target_title
					value = scope:target
				}
			}
		}
	}
}

pam_negotiate_land_possible_refusal_notificaton_effect = {
	if = {
		limit = {
			scope:target.tier >= scope:recipient.highest_held_title_tier_minus_one
			scope:recipient = {
				has_legitimacy = no
			}
		}
		scope:recipient = { custom_tooltip = refuse_ecclesiastic_land_transfer_penalty_no_legit_tt }
	}
	else_if = {
		limit = {
			scope:recipient = {
				has_legitimacy = no
			}
		}
		scope:recipient = { custom_tooltip = refuse_ecclesiastic_land_transfer_penalty_minor_no_legit_tt }
	}
	else_if = {
		limit = {
			scope:target.tier >= scope:recipient.highest_held_title_tier_minus_one
		}
		scope:recipient = { custom_tooltip = refuse_ecclesiastic_land_transfer_penalty_tt }
	}
	else = {
		scope:recipient = { custom_tooltip = refuse_ecclesiastic_land_transfer_penalty_minor_tt }
	}
}
clergy_land_refusal_legitimacy_piety_effect = {
	if = {
		limit = {
			scope:target.tier >= scope:recipient.highest_held_title_tier_minus_one
		}
		scope:recipient = {
			add_piety = clergy_land_refusal_large_piety_value
			if = {
				limit = {
					has_legitimacy = yes
				}
				add_legitimacy = clergy_land_refusal_large_legitimacy_value
			}
			else = {
				add_prestige = clergy_land_refusal_large_prestige_value
			}
		}
	}
	else = {
		scope:recipient = {
			add_piety = clergy_land_refusal_small_piety_value
			if = {
				limit = {
					has_legitimacy = yes
				}
				add_legitimacy = clergy_land_refusal_small_legitimacy_value
			}
			else = {
				add_prestige = clergy_land_refusal_small_prestige_value
			}
		}
	}
}

pam_available_negotiation_methods_tooltip_effect = {
	#show tooltips
	if = {
		limit = {
			pam_forced_negotiate_land_success_trigger = yes
		}
		#Do not show AI behaviour tooltips if this is recipient viewing
		if = {
			limit = {
				NOT = { exists = scope:recipient_event }
			}
			custom_tooltip = pam_clergy_land_forced_success_tt
			custom_tooltip = pam_negotiate_land_force_by_tt
		}
		#FORCED FIRST
		if = {
			limit = {
				pam_negotiate_land_forced_hook_trigger = yes
			}
			custom_tooltip = pam_forced_negotiate_land_hook_tt
		}
		if = {
			limit = {
				pam_negotiate_land_forced_relation_trigger = yes
			}
			custom_tooltip = pam_forced_negotiate_land_relation_tt
		}
		if = {
			limit = {
				pam_negotiate_land_forced_piety_level_trigger = yes
			}
			if = {
				limit = {
					scope:recipient.piety_level = 0
				}
				custom_tooltip = pam_forced_negotiate_land_piety_level_1_tt
			}
			else = {
				custom_tooltip = pam_forced_negotiate_land_piety_level_2_tt
			}
		}
		if = {
			limit = {
				pam_negotiate_land_forced_legitimacy_level_trigger = yes
			}
			if = {
				limit = {
					scope:recipient.legitimacy_level = 0
				}
				custom_tooltip = pam_forced_negotiate_land_legitimacy_level_1_tt
			}
			else = {
				custom_tooltip = pam_forced_negotiate_land_legitimacy_level_2_tt
			}
		}
		if = {
			limit = {
				pam_negotiate_land_forced_gold_trigger = yes
			}
			show_as_tooltip = {
				if = {
					limit = {
						has_treasury = yes
					}
					pay_short_term_treasury = {
						target = scope:recipient
						gold = scope:recipient.pam_transfer_land_gold_value
					}
				}
				else = {
					pay_short_term_gold = {
						target = scope:recipient
						gold = scope:recipient.pam_transfer_land_gold_value
					}
				}
			}
		}
	}
	else = {
		#Do not show AI behaviour tooltips if this is recipient viewing
		if = {
			limit = {
				NOT = { exists = scope:recipient_event }
			}
			custom_tooltip = pam_clergy_land_possible_success_tt
		}
	}

	if = {
		limit = {
			pam_possible_negotiate_land_success_trigger = yes
		}
		#Do not show AI behaviour tooltips if this is recipient viewing
		if = {
			limit = {
				NOT = { exists = scope:recipient_event }
			}
			custom_tooltip = pam_negotiate_land_possible_by_tt
		}
		#POSSIBLE NEXT
		if = {
			limit = {
				pam_negotiate_land_possible_hook_trigger = yes
				pam_negotiate_land_forced_hook_trigger = no
			}
			custom_tooltip = pam_possible_negotiate_land_hook_tt
		}
		if = {
			limit = {
				pam_negotiate_land_possible_relation_trigger = yes
				pam_negotiate_land_forced_relation_trigger = no
			}
			custom_tooltip = pam_possible_negotiate_land_relation_tt
		}
		if = {
			limit = {
				pam_negotiate_land_possible_piety_level_trigger = yes
				pam_negotiate_land_forced_piety_level_trigger = no
			}
			custom_tooltip = pam_possible_negotiate_land_piety_level_tt
		}
		if = {
			limit = {
				pam_negotiate_land_possible_legitimacy_level_trigger = yes
				pam_negotiate_land_forced_legitimacy_level_trigger = no
			}
			custom_tooltip = pam_possible_negotiate_land_legitimacy_level_tt
		}
		if = {
			limit = {
				pam_negotiate_land_possible_gold_trigger = yes
				pam_negotiate_land_forced_gold_trigger = no
			}
			show_as_tooltip = {
				pam_negotiate_land_gold_effect = yes
			}
		}
	}
}
pam_negotiate_land_hook_effect = {
	use_hook = scope:recipient
}
pam_negotiate_land_relation_accept_effect = {
	if = {
		limit = {
			has_relation_best_friend = scope:recipient
		}
		remove_relation_best_friend = scope:recipient
	}
	else_if = {
		limit = {
			has_relation_friend = scope:recipient
		}
		remove_relation_friend = scope:recipient
	}
	if = {
		limit = {
			has_relation_soulmate = scope:recipient
		}
		remove_relation_soulmate = scope:recipient
	}
	if = {
		limit = {
			has_relation_lover = scope:recipient
		}
		remove_relation_lover = scope:recipient
	}
	if = {
		limit = {
			has_relation_crush = scope:recipient
		}
		remove_relation_crush = scope:recipient
	}
	if = {
		limit = {
			has_relation_student = scope:recipient
		}
		remove_relation_student = scope:recipient
	}
	if = {
		limit = {
			has_relation_mentor = scope:recipient
		}
		remove_relation_mentor = scope:recipient
	}
	if = {
		limit = {
			has_relation_guardian = scope:recipient
		}
		remove_relation_guardian = scope:recipient
	}
	if = {
		limit = {
			has_relation_ward = scope:recipient
		}
		remove_relation_ward = scope:recipient
	}
	if = {
		limit = {
			has_relation_potential_friend = scope:recipient
		}
		remove_relation_potential_friend = scope:recipient
	}
	if = {
		limit = {
			has_relation_potential_lover = scope:recipient
		}
		remove_relation_potential_lover = scope:recipient
	}
	if = {
		limit = {
			has_relation_blood_brother = scope:recipient
		}
		remove_relation_blood_brother = scope:recipient
	}
	if = {
		limit = {
			has_relation_disciple = scope:recipient
		}
		remove_relation_disciple = scope:recipient
	}
	if = {
		limit = {
			has_relation_elder = scope:recipient
		}
		remove_relation_elder = scope:recipient
	}
}
pam_negotiate_land_relation_effect = {
	pam_negotiate_land_relation_accept_effect = yes
	if = {
		limit = {
			exists = scope:forceable_relation
		}
		custom_tooltip = recipient_becomes_potential_rival_tt
		hidden_effect = {
			set_relation_potential_rival = {
				target = scope:recipient
				reason = rival_quarreled_over_land
			}
		}
		change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
		scope:recipient = {
			change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
		}
	}
	else = {
		custom_tooltip = recipient_becomes_rival_tt
		hidden_effect = {
			set_relation_rival = {
				target = scope:recipient
				reason = rival_quarreled_over_land
			}
		}
		change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
		scope:recipient = {
			change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
		}
	}
}
pam_negotiate_land_piety_effect = {
	if = {
		limit = {
			pam_negotiate_land_forced_piety_level_trigger = yes
		}
		if = {
			limit = {
				scope:recipient.piety_level = 0
			}
			add_piety_experience = major_piety_loss
			scope:recipient = { add_piety_experience = massive_piety_loss }
		}
		else = {
			add_piety_experience = massive_piety_loss
			scope:recipient = { add_piety_level = -1 }
		}
	}
	else = {
		add_piety_level = -1
		scope:recipient = { add_piety_level = -2 }
	}
}
pam_negotiate_land_legitimacy_effect = {
	if = {
		limit = {
			pam_negotiate_land_forced_legitimacy_level_trigger = yes
		}
		if = {
			limit = {
				scope:recipient.legitimacy_level = 0
			}
			add_legitimacy = medium_legitimacy_loss
			scope:recipient = { add_legitimacy = major_legitimacy_loss }
		}
		else = {
			add_legitimacy = major_legitimacy_loss
			scope:recipient = { add_legitimacy_level = -1 }
		}
	}
	else = {
		add_legitimacy_level = -1
		scope:recipient = { add_legitimacy_level = -2 }
	}
}
pam_negotiate_land_gold_effect = {
	if = {
		limit = {
			has_treasury = yes
		}
		pay_short_term_treasury = {
			target = scope:recipient
			gold = scope:recipient.pam_transfer_land_gold_value
		}
	}
	else = {
		pay_short_term_gold = {
			target = scope:recipient
			gold = scope:recipient.pam_transfer_land_gold_value
		}
	}
}
pam_negotiate_land_tracking_variable_effect = {
	scope:recipient = {
		if = {
			limit = {
				has_variable = clergy_land_granted
				var:clergy_land_granted <= scope:target.tier
			}
			remove_variable = clergy_land_granted
			custom_tooltip = clergy_land_request_reduction_tt
			set_variable = {
				name = clergy_land_granted
				value = scope:target.tier
				years = 5
			}
		}
		else_if = {
			limit = {
				NOT = {
					has_variable = clergy_land_granted
				}
			}
			custom_tooltip = clergy_land_request_reduction_tt
			set_variable = {
				name = clergy_land_granted
				value = scope:target.tier
				years = 5
			}
		}
	}
}

######################
### RITE DIVERGENCE DRIFT
######################


@hor_drift_forward_cap = 40
@hor_drift_backward_floor = 5

@hor_drift_step_high = 20
@hor_drift_step_normal = 10
@hor_drift_step_low = 5

@hor_drift_step_high_neg = -20
@hor_drift_step_normal_neg = -10
@hor_drift_step_low_neg = -5

@hor_drift_low_fervor = 40
@hor_drift_high_fervor = 70

rite_yearly_hor_divergence_drift_effect = {
	if = {
		limit = {
			this != faith.main_rite
			# We leave rites that are already heretical alone, since heresy already covers that range
			"divergence(faith.main_rite)" < faith.heresy_threshold
			head_of_rite ?= {
				is_available_ai_adult = yes
				NOT = { has_character_flag = hor_already_drifted_rite_flag }
			}
		}
		random = {
			chance = {
				value = 4
				# Low fervor: lots of room for reinterpretation
				if = {
					limit = { faith.fervor <= @hor_drift_low_fervor }
					add = 6
				}
				# Stable faith: rare drift
				if = {
					limit = { faith.fervor >= @hor_drift_high_fervor }
					add = -2
				}
				# Unfulfilled HoR's do changes more often
				if = {
					limit = {
						head_of_rite ?= {
							spiritual_fulfillment <= 0
						}
					}
					add = 4
				}
				if = {
					limit = { head_of_rite ?= { has_trait = heresiarch } }
					add = 25
				}
				min = 1
			}

			rite_yearly_hor_divergence_drift_apply_effect = yes
		}
	}
}

rite_yearly_hor_divergence_drift_apply_effect = {
	save_scope_as = diverged_rite
	head_of_rite = { save_scope_as = diverged_hor }

	# Snapshot the rite's current tenets and (HoR-known) doctrines so we can identify what change_rite_divergence swapped in.
	every_rite_tenet = {
		add_to_list = drift_tenets_before
	}
	scope:diverged_hor = {
		every_character_doctrine = {
			rite_filter = scope:diverged_rite
			add_to_list = drift_doctrines_before
		}
	}

	random_list = {
		100 = {
			trigger = {
				"divergence(faith.main_rite)" <= 40
			}
			modifier = {
				factor = 0.5
				"divergence(faith.main_rite)" >= 30
			}
			modifier = {
				# Low fervor faiths drift outward more readily.
				factor = 1.5
				faith.fervor <= @hor_drift_low_fervor
			}
			modifier = {
				factor = 0.6
				faith.fervor >= @hor_drift_high_fervor
			}

			save_scope_value_as = { name = diverged_change value = 1 }
			rite_yearly_hor_divergence_drift_forward_pick_effect = yes
		}
		100 = {
			trigger = {
				"divergence(faith.main_rite)" >= 10
				faith.rite_growth_rites_needed <= 0
				trigger_if = {
					limit = {
						exists = faith.religious_head
					}
					scope:diverged_hor = {
						OR = {
							opinion = {
								target = scope:diverged_rite.faith.religious_head
								value >= 50
							}
							has_dread_level_towards = {
								target = scope:diverged_rite.faith.religious_head
								level >= 1
							}
						}
					}
				}
				trigger_else = {
					always = yes
				}
			}
			modifier = {
				# Mirror the forward throttle: when divergence is small there's less room to walk it back.
				factor = 0.5
				"divergence(faith.main_rite)" < 20
			}
			modifier = {
				factor = 1.3
				faith.fervor >= @hor_drift_high_fervor
			}
			modifier = {
				# Consolidation belongs to high-fervor faiths; sub-70 fervor is the divergence regime.
				factor = 0.2
				faith.fervor < @hor_drift_high_fervor
			}
			modifier = {
				# Without a HoF to anchor consolidation, backward drift is much rarer.
				factor = 0.2
				NOT = { exists = faith.religious_head }
			}
			save_scope_value_as = { name = diverged_change value = -1 }
			rite_yearly_hor_divergence_drift_backward_pick_effect = yes
		}
	}
}

rite_yearly_hor_divergence_drift_forward_pick_effect = {
	random_list = {
		1 = {
			# Only allow a +20 jump if we're far below the cap.
			trigger = {
				"divergence(faith.main_rite)" <= 20
			}
			modifier = {
				factor = 0.4
				"divergence(faith.main_rite)" >= 15
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_high }
			change_rite_divergence = {
				value = @hor_drift_step_high
				override_tenets_first = no
			}
		}
		1 = {
			trigger = {
				"divergence(faith.main_rite)" <= 20
			}
			modifier = {
				factor = 0.4
				"divergence(faith.main_rite)" >= 15
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_high }
			change_rite_divergence = {
				value = @hor_drift_step_high
				override_tenets_first = yes
			}
		}
		3 = {
			trigger = {
				"divergence(faith.main_rite)" <= 30
			}
			modifier = {
				factor = 0.5
				"divergence(faith.main_rite)" >= 25
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_normal }
			change_rite_divergence = {
				value = @hor_drift_step_normal
				override_tenets_first = no
			}
		}
		3 = {
			trigger = {
				"divergence(faith.main_rite)" <= 30
			}
			modifier = {
				factor = 0.5
				"divergence(faith.main_rite)" >= 25
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_normal }
			change_rite_divergence = {
				value = @hor_drift_step_normal
				override_tenets_first = yes
			}
		}
		8 = {
			trigger = {
				"divergence(faith.main_rite)" <= 40
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_low }
			change_rite_divergence = {
				value = @hor_drift_step_low
				override_tenets_first = no
			}
		}
		8 = {
			trigger = {
				"divergence(faith.main_rite)" <= 40
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_low }
			change_rite_divergence = {
				value = @hor_drift_step_low
				override_tenets_first = yes
			}
		}
	}
	rite_yearly_hor_divergence_drift_commit_effect = yes
}

rite_yearly_hor_divergence_drift_backward_pick_effect = {
	random_list = {
		1 = {
			trigger = {
				"divergence(faith.main_rite)" >= 25
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_high_neg }
			change_rite_divergence = {
				value = @hor_drift_step_high_neg
				override_tenets_first = no
			}
		}
		1 = {
			trigger = {
				"divergence(faith.main_rite)" >= 25
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_high_neg }
			change_rite_divergence = {
				value = @hor_drift_step_high_neg
				override_tenets_first = yes
			}
		}
		3 = {
			trigger = {
				"divergence(faith.main_rite)" >= 15
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_normal_neg }
			change_rite_divergence = {
				value = @hor_drift_step_normal_neg
				override_tenets_first = no
			}
		}
		3 = {
			trigger = {
				"divergence(faith.main_rite)" >= 15
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_normal_neg }
			change_rite_divergence = {
				value = @hor_drift_step_normal_neg
				override_tenets_first = yes
			}
		}
		8 = {
			trigger = {
				"divergence(faith.main_rite)" >= 10
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_low_neg }
			change_rite_divergence = {
				value = @hor_drift_step_low_neg
				override_tenets_first = no
			}
		}
		8 = {
			trigger = {
				"divergence(faith.main_rite)" >= 10
			}
			save_scope_value_as = { name = diverged_change value = @hor_drift_step_low_neg }
			change_rite_divergence = {
				value = @hor_drift_step_low_neg
				override_tenets_first = yes
			}
		}
	}
	rite_yearly_hor_divergence_drift_commit_effect = yes
}

rite_yearly_hor_divergence_drift_commit_effect = {
	random_rite_tenet = {
		limit = {
			NOT = { is_in_list = drift_tenets_before }
		}
		save_scope_as = diverged_added_tenet
	}
	scope:diverged_hor = {
		random_character_doctrine = {
			rite_filter = scope:diverged_rite
			limit = {
				NOT = { is_in_list = drift_doctrines_before }
			}
			save_scope_as = diverged_added_doctrine
		}
	}

	if = {
		limit = {
			OR = {
				exists = scope:diverged_added_tenet
				exists = scope:diverged_added_doctrine
			}
		}
		scope:diverged_hor = {
			add_character_flag = hor_already_drifted_rite_flag
		}
		if = {
			limit = { scope:diverged_change <= -1 }
			scope:diverged_rite.faith = {
				change_fervor = {
					value = notable_fervor_value
					desc = fervor_gain_pam_rite_consolidated
				}
			}
		}
		else_if = {
			limit = { scope:diverged_change >= 1 }
			scope:diverged_rite.faith = {
				change_fervor = {
					value = significant_fervor_loss
					desc = fervor_loss_pam_rite_diverged
				}
			}
		}
		rite_yearly_hor_divergence_drift_notify_effect = yes
	}
}

# Notify every ruler of the rite + the religious head, via letter event.
rite_yearly_hor_divergence_drift_notify_effect = {
	faith = {
		every_faith_ruler = {
			limit = {
				rite = scope:diverged_rite
				this != scope:diverged_hor
			}
			trigger_event = pam_tenet_events.0004
		}
	}
	# Religious head (if any) gets the letter too, even if they aren't iterated above.
	faith.religious_head ?= {
		if = {
			limit = {
				NOT = { rite = scope:diverged_rite }
				this != scope:diverged_hor
			}
			trigger_event = pam_tenet_events.0004
		}
	}
}

pam_declare_rite_heresy_effect = {
	custom_tooltip = {
		text = pam_declare_rite_heresy_effect_tt
		detach_rite_to_new_faith = {
			target = scope:heresy_rite
		}
	}
	# Heresy rite types ship convert = no so they cannot be joined before the bull; branding has to flip it or nothing can convert in.
	scope:heresy_rite = {
		set_can_convert_to_rite = yes
	}
	faith = {
		change_fervor = {
			value = notable_fervor_value
			desc = fervor_gain_pam_rite_declared_heresy
		}
		set_variable = {
			name = rite_growth_recent_heresy
			value = yes
			days = rite_growth_heresy_cooldown_days
		}
	}
	# The declaring faith repudiates the now-heretical rite, so its core tenets lose standing there.
	if = {
		limit = { faith = { has_tenet_popularity_trigger = yes  } }
		faith = { save_scope_as = heresy_declaring_faith }
		scope:heresy_rite = {
			every_rite_tenet = {
				status = core
				save_scope_as = heresy_rite_tenet
				scope:heresy_declaring_faith = {
					change_tenet_popularity = {
						target = scope:heresy_rite_tenet
						change = major_tenet_popularity_loss
					}
				}
			}
		}
	}
	if = {
		limit = {
			exists = scope:heresy_rite
			NOT = { scope:heresy_rite.faith = faith }
		}
		scope:heresy_rite.faith = {
			every_faith_character = {
				limit = {
					is_ruler = yes
					this != scope:bull_issuer
				}
				trigger_event = { id = pam_decision_events.0013 days = 1 }
			}
		}
	}
}

#This is only for use in pam_cathedral_01, pam_cathedral_02, and pam_cathedral_03
pam_cathedral_art_contribution_subject_event_effect = {
	save_scope_as = contributor
	scope:contributor = {
		trigger_event = {
			id = $EVENT$
			days = { 5 20 }
		}
	}
}

#This is only for use in pam_cathedral_01, pam_cathedral_02, and pam_cathedral_03
cathedral_great_project_funded_started_event_sender_effect = {
	scope:founder = {
		trigger_event = pam_great_projects_events.0005
	}
	scope:owner = {
		if = {
			limit = {
				this != scope:founder
			}
			trigger_event = pam_great_projects_events.0005
		}
	}
	every_contribution = {
		limit = {
			contribution_is_funded = yes
		}
		contributor ?= {
			if = {
				limit = {
					this != scope:owner
					this != scope:founder
				}
				save_scope_as = contributor
				trigger_event = pam_great_projects_events.0005
			}
		}
	}
}

cathedral_great_project_completed_event_sender_effect = {
	scope:founder = {
		trigger_event = pam_great_projects_events.0020
	}
	scope:owner = {
		if = {
			limit = {
				this != scope:founder
			}
			trigger_event = pam_great_projects_events.0020
		}
	}
	scope:founder_heir ?= {
		if = {
			limit = {
				is_alive = yes
				this != scope:founder
				this != scope:owner
			}
			trigger_event = pam_great_projects_events.0020
		}
	}
	every_contribution = {
		limit = {
			contribution_is_funded = yes
		}
		contributor ?= {
			if = {
				limit = {
					this != scope:owner
					this != scope:founder
					NOT = { this ?= scope:founder_heir }
				}
				save_scope_as = contributor
				trigger_event = pam_great_projects_events.0020
			}
		}
	}
}

# Create a New Ecclesiastical Title as HoF - on_accept
pam_create_new_ecclesiastical_title_interaction_effect = {
	scope:secondary_recipient = {
		save_scope_as = new_ecclesiastical_holder
	}
	scope:recipient = {
		save_scope_as = kingdom_holder
		if = {
			limit = {
				scope:new_ecclesiastical_holder = {
					NOT = {
						is_courtier_of = scope:kingdom_holder
					}
					is_ruler = no
				}
			}
			add_courtier = scope:new_ecclesiastical_holder
		}
		if = {
			limit = {
				scope:new_ecclesiastical_holder = {
					NOT = { is_councillor_of = scope:kingdom_holder }
				}
			}
			assign_councillor_type = {
				type = councillor_court_chaplain
				remove_existing_councillor = yes
				target = scope:new_ecclesiastical_holder
			}
		}
	}
	scope:target = {
		save_scope_as = target_kingdom
	}
	scope:actor = {
		if = {
			limit = {
				any_character_situation = {
					situation_type = the_christian_church
					situation_top_has_catalyst = catalyst_the_christian_church_clerical_region_created
				}
			}
			situation:the_christian_church = {
				trigger_situation_catalyst = catalyst_the_christian_church_clerical_region_created
			}
		}
		send_interface_toast = {
			type = event_toast_effect_good
			title = create_new_ecclesiastical_title_toast
			left_icon = scope:actor
			right_icon = scope:new_ecclesiastical_holder
			custom_tooltip = {
				text = create_new_ecclesiastical_title_effect_tt
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				scope:target_kingdom = {
					title_capital_county = {
						save_scope_as = clerical_region_anchor_county
						random_county_province = {
							limit = {
								is_county_capital = yes
							}
							save_scope_as = region_capital
							create_clerical_region = {
								domicile_type = ecclesiastical_domicile
								name = NEW_ECCLESIASTICAL_TITLE
								save_scope_as = new_title
							}
							scope:new_title ?= {
								set_coa = scope:target_kingdom
								set_color_from_title = scope:target_kingdom
								set_capital_county = scope:clerical_region_anchor_county
								set_landless_title = yes
								set_no_automatic_claims = yes
								set_definitive_form = yes
								change_title_holder = {
									holder = scope:new_ecclesiastical_holder
									change = scope:change
								}
								resolve_title_and_vassal_change = scope:change
							}
						}
					}
				}
			}
		}
		reverse_add_opinion = {
			target = scope:new_ecclesiastical_holder
			modifier = grateful_opinion
			opinion = 50
		}
	}
	scope:new_ecclesiastical_holder = {
		if = {
			limit = {
				NOT = { government_has_flag = government_is_ecclesiastical }
			}
			change_government = ecclesiastical_government
		}
		faith = {
			change_fervor = {
				value = notable_fervor_gain
				desc = fervor_gain_clerical_title_created
			}
		}
		situation:the_christian_church ?= {
			add_manual_participant = prev
		}
	}
}

# Expects scope:target and scope:pam_new_archbishop, and the caller must save pam_old_archbishop first
pam_grant_ecclesiastical_title_interaction_effect = {
	scope:actor = {
		send_interface_toast = {
			type = event_toast_effect_good
			title = pam_grant_ecclesiastical_title_toast
			left_icon = scope:actor
			right_icon = scope:pam_new_archbishop
			if = {
				limit = { exists = scope:pam_old_archbishop }
				custom_tooltip = {
					text = pam_grant_ecclesiastical_title_deposed_effect_tt
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					scope:pam_old_archbishop = {
						every_held_title = {
							limit = {
								OR = {
									has_clerical_region = yes
									is_landless_type_title = no
								}
							}
							change_title_holder_include_vassals = {
								holder = scope:pam_new_archbishop
								change = scope:change
								government_base = scope:pam_old_archbishop
							}
						}
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
			else = {
				custom_tooltip = {
					text = pam_grant_ecclesiastical_title_vacant_effect_tt
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
					}
					scope:target = {
						change_title_holder_include_vassals = {
							holder = scope:pam_new_archbishop
							change = scope:change
						}
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
			add_hook = {
				type = granted_clerical_title_hook
				target = scope:pam_new_archbishop
			}
		}
		reverse_add_opinion = {
			target = scope:pam_new_archbishop
			modifier = grateful_opinion
			opinion = 50
		}
	}
	scope:pam_old_archbishop ?= {
		add_opinion = {
			target = scope:actor
			modifier = fp3_resentful_opinion
			opinion = -40
		}
	}
	if = {
		limit = {
			scope:pam_old_archbishop ?= { rite != scope:actor.rite }
		}
		scope:actor.faith = {
			change_fervor = {
				value = significant_fervor_loss
				desc = fervor_loss_pam_archbishop_deposed
			}
		}
	}
	scope:pam_new_archbishop = {
		if = {
			limit = {
				NOT = { government_has_flag = government_is_ecclesiastical }
			}
			change_government = ecclesiastical_government
		}
		situation:the_christian_church ?= {
			add_manual_participant = prev
		}
	}
	if = {
		limit = {
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_clergy_elevated_to_archbishopric
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_clergy_elevated_to_archbishopric
		}
	}
}

# Picks scope:target_character for a GHW after scope:target_kingdom is already set (e.g. bull kingdom selection).
pam_determine_ghw_target_character_from_kingdom_effect = {
	scope:target_kingdom = {
		random_in_de_jure_hierarchy = {
			continue = { tier > tier_county }
			limit = {
				tier = tier_county
				$FAITH$.main_rite = {
					is_hostile_enough_for_holy_war_trigger = { RITE = prev.holder.rite }
				}
				holder = {
					highest_held_title_tier >= 4
					any_realm_county = { count >= 5 }
				}
			}
			alternative_limit = {
				tier = tier_county
				$FAITH$.main_rite = {
					is_hostile_enough_for_holy_war_trigger = { RITE = prev.holder.rite }
				}
				holder = {
					highest_held_title_tier >= 3
					any_realm_county = { count >= 5 }
				}
			}
			alternative_limit = {
				tier = tier_county
				$FAITH$.main_rite = {
					is_hostile_enough_for_holy_war_trigger = { RITE = prev.holder.rite }
				}
				holder = {
					highest_held_title_tier >= 2
					any_realm_county = { count >= 5 }
				}
			}
			alternative_limit = {
				tier = tier_county
				$FAITH$.main_rite = {
					is_hostile_enough_for_holy_war_trigger = { RITE = prev.holder.rite }
				}
			}
			alternative_limit = {
				tier = tier_kingdom
				$FAITH$.main_rite = {
					is_hostile_enough_for_holy_war_trigger = { RITE = prev.holder.rite }
				}
			}
			holder = {
				save_scope_as = target_character
			}
		}
	}
}

pam_use_bull_selected_ghw_target_effect = {
	$GHW_SPONSOR$ = {
		var:pam_bull_ghw_target_kingdom = { save_scope_as = target_kingdom }
		remove_variable = pam_bull_ghw_target_kingdom
	}
	pam_determine_ghw_target_character_from_kingdom_effect = { FAITH = $FAITH$ }
}

# Starts an undirected GHW using the kingdom stored on the HoF by pick_crusade_target_decision.
pam_start_bull_great_holy_war_effect = {
	$GHW_SPONSOR$ = {
		if = {
			limit = { has_variable = pam_bull_ghw_target_kingdom }
			religion = { set_variable = variable_ghw_unlocked }
			$FAITH$ = { trigger_event = great_holy_war.0001 }
		}
	}
}

pam_bull_notify_faith_rulers_effect = {
	scope:bull_issuer = {
		faith = {
			every_faith_character = {
				limit = {
					is_ruler = yes
					this != scope:bull_issuer
				}
				trigger_event = { id = pam_bull_events.0004 days = 1 }
			}
		}
	}
}

pam_apply_great_schism_creation_effect = {
	if = {
		limit = { NOT = { has_global_variable = pam_great_schism_happened } }
		set_global_variable = {
			name = pam_great_schism_happened
			value = current_year
		}
	}
	situation:the_christian_church.var:schism_mainline_rite = { save_scope_as = schism_mainline_rite }
	if = {
		limit = { scope:schism_mainline_rite = rite:roman_rite }
		faith:catholic = { save_scope_as = schism_mainline_faith }
		faith:orthodox = { save_scope_as = schism_competitor_faith }
	}
	else = {
		faith:orthodox = { save_scope_as = schism_mainline_faith }
		faith:catholic = { save_scope_as = schism_competitor_faith }
	}
	custom_tooltip = {
		text = pam_create_great_schism_effect_tt
		faith:christian_faith = {
			save_scope_as = old_rite
			every_faith_rite = {
				limit = { rite_has_doctrine = doctrine_no_head }
				change_rite_doctrine = doctrine_spiritual_head
			}
		}
		faith:catholic = { 
			set_religious_head_title = title:k_papal_state
		}
		faith:orthodox = { 
			set_religious_head_title = title:k_orthodox
		}
		rite:roman_rite = {
			save_scope_as = roman_rite
			set_can_convert_to_rite = yes
			set_parent_faith = {
				target = faith:catholic
				main = yes
			}
		}
		rite:insular_celtic = {
			set_can_convert_to_rite = yes
			set_parent_faith = {
				target = faith:catholic
				main = no
			}
		}
		rite:mozarabic_church = {
			set_can_convert_to_rite = yes
			set_parent_faith = {
				target = faith:catholic
				main = no
			}
		}
		rite:ambrosian_rite = {
			set_can_convert_to_rite = yes
			set_parent_faith = {
				target = faith:catholic
				main = no
			}
		}
		rite:byzantine_rite = {
			save_scope_as = byzantine_rite
			set_can_convert_to_rite = yes
			set_parent_faith = {
				target = faith:orthodox
				main = yes
			}
		}
		rite:georgian_rite = {
			set_can_convert_to_rite = yes
			set_parent_faith = {
				target = faith:orthodox
				main = no
			}
		}
		rite:slavic_rite = {
			set_can_convert_to_rite = yes
			set_parent_faith = {
				target = faith:orthodox
				main = no
			}
		}
		rite:carolingian_christianity = {
			set_can_convert_to_rite = no
			set_rite_enabled = no
			set_parent_faith = {
				target = faith:catholic
				main = no
			}
		}
		rite:beneventan_rite = {
			set_can_convert_to_rite = no
			set_rite_enabled = no
			set_parent_faith = {
				target = faith:catholic
				main = no
			}
		}
		struggle:iberian_struggle ?= {
			set_faith_as_involved = rite:mozarabic_church.faith
			set_faith_as_uninvolved = faith:christian_faith
		}
	}
	show_as_tooltip = {
		faith:catholic = { set_religious_head_title = title:k_papal_state }
	}
	hidden_effect = {
		situation:the_christian_church ?= {
			every_situation_participant = {
				limit = {
					OR = {
						var:pam_schism_stance ?= flag:mainline
						var:pam_schism_stance ?= flag:competitor
					}
					OR = {
						faith = faith:christian_faith
						faith = scope:schism_mainline_faith
						faith = scope:schism_competitor_faith
					}
					NOR = {
						any_held_title = { is_head_of_faith = yes }
						primary_title ?= { is_holy_order = yes }
					}
				}
				if = {
					limit = {
						var:pam_schism_stance ?= flag:mainline
						NOT = { faith = scope:schism_mainline_faith }
					}
					set_character_faith = scope:schism_mainline_faith
				}
				else_if = {
					limit = {
						var:pam_schism_stance ?= flag:competitor
						NOT = { faith = scope:schism_competitor_faith }
					}
					set_character_faith = scope:schism_competitor_faith
				}
			}
		}
		faith:christian_faith = {
			every_clerical_elector_title = {
				add_to_list = pam_schism_cardinalates
			}
		}
		every_in_list = {
			list = pam_schism_cardinalates
			save_temporary_scope_as = pam_schism_cardinalate
			if = {
				limit = {
					holder ?= {
						OR = {
							faith = faith:catholic
							AND = {
								faith = faith:christian_faith
								OR = {
									rite = rite:roman_rite
									rite = rite:insular_celtic
									rite = rite:mozarabic_church
									rite = rite:ambrosian_rite
								}
							}
						}
					}
				}
				holder = {
					if = {
						limit = { NOT = { faith = faith:catholic } }
						set_character_faith = faith:catholic
					}
				}
				add_clerical_elector = faith:catholic
			}
			else_if = {
				limit = { exists = holder }
				holder = {
					pam_destroy_cardinal_title_effect = { CARDINALATE = scope:pam_schism_cardinalate }
				}
			}
		}
		faith:christian_faith = { remove_doctrine = special_doctrine_has_clerical_electors }
		# Catholicism starts with a College so AI clergy do not spam the Pope to fill vacancies
		faith:catholic = { pam_seed_cardinals_effect = yes }
		if = {
			limit = { exists = situation:the_christian_church }
			the_christian_church_set_main_power_faith_effect = yes
			the_christian_church_recalculate_participant_groups_effect = yes
		}
	}
}

# Rite followers move with their rites
pam_unite_miaphysite_churches_effect = {
	save_scope_as = unifier
	if = {
		limit = { faith = faith:miaphysitism }
		custom_tooltip = {
			text = pam_unite_miaphysite_churches_effect_tt
			faith = { save_scope_as = uniting_faith }
			faith:armenian_apostolic = {
				save_scope_as = absorbed_faith
				remove_religious_head_title = yes
				every_faith_rite = {
					limit = { faith != scope:uniting_faith }
					set_parent_faith = {
						target = faith:miaphysitism
						main = no
						include_derived = no
					}
				}
			}
		}
	}
	else = {
		custom_tooltip = {
			text = pam_unite_miaphysite_churches_effect_tt
			faith = { save_scope_as = uniting_faith }
			faith:miaphysitism = {
				save_scope_as = absorbed_faith
				remove_religious_head_title = yes
				every_faith_rite = {
					limit = { faith != scope:uniting_faith }
					set_parent_faith = {
						target = faith:armenian_apostolic
						main = no
						include_derived = no
					}
				}
			}
		}
	}
	faith = {
		change_fervor = {
			value = medium_fervor_gain
			desc = fervor_gain_mended_schism
		}
	}
	hidden_effect = {
		every_player = {
			limit = {
				this != root
				faith = scope:uniting_faith
			}
			send_interface_message = {
				type = event_generic_good_with_text
				title = pam_unite_miaphysite_churches_message
				desc = pam_unite_miaphysite_churches_message_desc
				left_icon = scope:unifier
			}
		}
	}
	set_nickname_effect = { NICKNAME = nick_the_ecumenist }
}

pam_great_schismat_effect = {
	give_nickname = nick_the_schismat
	dynasty ?= {
		add_dynasty_modifier = { modifier = pam_caused_great_schism_dynasty_modifier }
	}
	if = {
		limit = { NOT = { exists = dynasty } }
		add_character_modifier = pam_caused_great_schism_character_modifier
	}
	# 0061 already announces the Schism
	set_global_variable = {
		name = pam_skip_tcc_phase_start_event
		days = 30
	}
	situation:the_christian_church = {
		change_top_phase = { phase = concord }
	}
	save_scope_as = schismat
}

pam_create_great_schism_effect = {
	set_global_variable = {
		name = pam_skip_tcc_phase_start_event
		days = 30
	}
	pam_apply_great_schism_creation_effect = yes
	set_global_variable = pam_great_schism_decision_taken
	faith:catholic = { save_scope_as = schism_western_faith }
	faith:orthodox = { save_scope_as = schism_eastern_faith }
	if = {
		limit = { NOT = { exists = scope:schismat } }
		situation:the_christian_church = {
			change_top_phase = { phase = reform_phase }
		}
	}
	situation:the_christian_church ?= {
		every_situation_participant = {
			limit = {
				is_ruler = yes
				faith.religion = religion:christianity_religion
			}
			trigger_event = pam_the_christian_church.0061
		}
	}
}

pam_prevent_great_schism_effect = {
	custom_tooltip = pam_prevent_great_schism_aftermath_tt
	give_nickname = nick_the_concordant
	dynasty ?= {
		add_dynasty_modifier = { modifier = pam_prevented_great_schism_dynasty_modifier }
	}
	if = {
		limit = { NOT = { exists = dynasty } }
		add_character_modifier = pam_prevented_great_schism_character_modifier
	}
	# 0062 already announces the Schism
	set_global_variable = {
		name = pam_skip_tcc_phase_start_event
		days = 30
	}
	situation:the_christian_church = {
		change_top_phase = { phase = concord }
	}
}

# Run from either Mend the Great Schism decision, which pass the modifier pair matching their route.
# MODIFIER - dynasty modifier for the mender's dynasty.
# CHARACTER_MODIFIER - equivalent for a lowborn mender, who has no dynasty to mark.
pam_mend_great_schism_reward_effect = {
	dynasty ?= {
		add_dynasty_prestige = 10000
		add_dynasty_modifier = { modifier = $MODIFIER$ }
	}
	if = {
		limit = { NOT = { exists = dynasty } }
		add_character_modifier = $CHARACTER_MODIFIER$
	}
	add_piety = monumental_piety_gain
	add_prestige = monumental_prestige_gain
	set_nickname_effect = { NICKNAME = nick_the_reconciler }
}

# Character scope, saved so the decision tooltip, 0062 option, and seating all name the same see
# Falls back to Rome so an unheld Constantinople does not leave the faith headless
pam_save_schism_head_of_faith_effect = {
	if = {
		limit = {
			rite = rite:byzantine_rite
			exists = title:d_et_constantinople.holder
		}
		title:d_et_constantinople = { save_scope_as = new_head_of_faith_title }
	}
	else_if = {
		limit = {
			rite = rite:byzantine_rite
			exists = title:k_orthodox.holder
		}
		title:k_orthodox = { save_scope_as = new_head_of_faith_title }
	}
	else_if = {
		limit = { exists = title:k_papal_state.holder }
		title:k_papal_state = { save_scope_as = new_head_of_faith_title }
	}
	scope:new_head_of_faith_title.holder ?= { save_scope_as = new_head_of_faith }
}

# Needs pam_save_schism_head_of_faith_effect
pam_schism_seat_head_of_faith_effect = {
	if = {
		limit = { exists = scope:new_head_of_faith_title }
		custom_tooltip = {
			text = pam_schism_seat_head_of_faith_tt
			faith:christian_faith = {
				if = {
					limit = { NOT = { has_doctrine = doctrine_spiritual_head } }
					change_doctrine = doctrine_spiritual_head
				}
				set_religious_head_title = scope:new_head_of_faith_title
				# Carolingian Christianity has doctrine_no_head; leave it behind once the faith has a head
				hidden_effect = {
					every_faith_rite = {
						if = {
							limit = { rite_has_doctrine = doctrine_no_head }
							change_rite_doctrine = doctrine_spiritual_head
						}
						force_refresh_head_of_rite = yes
					}
				}
			}
		}
	}
}

# The seat kept vacant. Chalcedonianism is already headless and keeps doctrine_spiritual_head, so
# there is nothing to change - doctrine_no_head would bar Establish a Head of Faith for good.
pam_schism_leave_faith_headless_effect = {
	custom_tooltip = pam_schism_leave_faith_headless_tt
}

# Apply an ancestral Khachkar in the chosen county (for decision confirmation event).
pam_apply_ancestral_khachkar_effects = {
	$LOCATION$ = {
		save_scope_as = khachkar_location
		set_variable = {
			name = pam_khachkar_ancestor
			value = $ANCESTOR$
		}
		set_variable = {
			name = pam_khachkar_commissioner
			value = root
		}
		add_county_modifier = {
			modifier = pam_khachkar_ancestral_county
			desc = pam_khachkar_ancestral_county_custom_desc
		}
	}
	add_prestige = medium_prestige_value
	add_piety = medium_piety_value
	add_legitimacy = 100
	remove_variable = pam_khachkar_ancestral_ancestor
}

# Remember a same-faith expansion outside the see's home kingdom so later petitions are harder to accept
pam_mark_recent_same_faith_clerical_expansion_effect = {
	if = {
		limit = {
			exists = $TITLE$
			pam_expand_target_is_same_faith_outside_home_kingdom_trigger = {
				TITLE = $TITLE$
				ARCHBISHOP = $ARCHBISHOP$
			}
		}
		$ARCHBISHOP$ = {
			hidden_effect = {
				add_character_flag = {
					flag = pam_recently_expanded_same_faith
					years = pam_expand_same_faith_recent_years
				}
			}
		}
	}
}

# Expand an Ecclesiastical Title as HoF - on_accept
pam_expand_ecclesiastical_title_interaction_effect = {
	scope:recipient = {
		save_scope_as = clerical_region_holder
		# We use the picker's region and skip alternative_limit, so we do not land on a non-bordering see
		if = {
			limit = {
				capital_province.county.clerical_region_title ?= {
					has_clerical_region = yes
					holder ?= scope:clerical_region_holder
				}
			}
			capital_province.county.clerical_region_title = {
				save_scope_as = clerical_region_title
			}
		}
	}
	scope:target = {
		save_scope_as = target_duchy
	}
	# is_adjacent_to_clerical_region matches the counties expand_clerical_region would take
	if = {
		limit = {
			exists = scope:clerical_region_title
			scope:clerical_region_title = { is_adjacent_to_clerical_region = scope:target_duchy }
		}
		scope:actor = {
			if = {
				limit = {
					any_character_situation = {
						situation_type = the_christian_church
						situation_top_has_catalyst = catalyst_the_christian_church_clerical_region_expanded
					}
				}
				situation:the_christian_church = {
					trigger_situation_catalyst = catalyst_the_christian_church_clerical_region_expanded
				}
			}
			hidden_effect = {
				send_interface_toast = {
					type = event_toast_effect_good
					title = expand_ecclesiastical_title_toast
					left_icon = scope:clerical_region_holder
					right_icon = scope:clerical_region_title
					custom_tooltip = {
						text = expand_ecclesiastical_title_effect_tt
						scope:clerical_region_title = {
							expand_clerical_region = scope:target_duchy
						}
					}
				}
			}
			faith = {
				change_fervor = {
					value = notable_fervor_gain
					desc = fervor_gain_clerical_title_expanded
				}
			}
		}
		scope:clerical_region_holder = {
			if = {
				limit = {
					NOT = { this = scope:actor }
				}
				add_opinion = {
					target = scope:actor
					modifier = grateful_opinion
					opinion = 50
				}
			}
			send_interface_toast = {
				type = event_toast_effect_good
				title = expand_ecclesiastical_title_toast
				left_icon = scope:actor
				right_icon = scope:clerical_region_title
				custom_tooltip = expand_ecclesiastical_title_effect_tt
			}
			pam_mark_recent_same_faith_clerical_expansion_effect = {
				TITLE = scope:target_duchy
				ARCHBISHOP = this
			}
		}
	}
}

# Split an Ecclesiastical Title as HoF - on_accept
pam_split_ecclesiastical_title_interaction_effect = {
	scope:secondary_recipient = {
		save_scope_as = new_ecclesiastical_holder
	}
	scope:secondary_actor = {
		save_scope_as = secular_ruler
	}
	scope:recipient = {
		save_scope_as = clerical_region_holder
	}
	scope:secular_ruler.capital_county.clerical_region_title ?= {
		save_scope_as = clerical_region_title
	}
	# Interaction tooltip reads on_accept before the split
	show_as_tooltip = {
		pam_split_ecclesiastical_title_outcome_effect = yes
	}
	hidden_effect = {
		scope:secular_ruler = {
			capital_province = { save_scope_as = region_capital }
			split_clerical_region = {
				name = CLERICAL_REGION_PROVINCE_TITLE_NAME
				save_scope_as = new_title
			}
		}
		# A failed split leaves scope:new_title unset
		if = {
			limit = { exists = scope:new_title }
			pam_split_ecclesiastical_title_outcome_effect = yes
		}
	}
}

# Runs twice, once for the interaction window tooltip and once for real after a successful split
pam_split_ecclesiastical_title_outcome_effect = {
	scope:secular_ruler = {
		if = {
			limit = {
				scope:new_ecclesiastical_holder = {
					NOT = {
						is_courtier_of = scope:secular_ruler
					}
					is_ruler = no
				}
			}
			add_courtier = scope:new_ecclesiastical_holder
		}
		if = {
			limit = {
				scope:new_ecclesiastical_holder = {
					NOT = { is_councillor_of = scope:secular_ruler }
				}
			}
			assign_councillor_type = {
				type = councillor_court_chaplain
				remove_existing_councillor = yes
				target = scope:new_ecclesiastical_holder
			}
		}
	}
	hidden_effect = {
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
			add_claim_on_loss = no
		}
		scope:new_title ?= {
			set_coa = scope:secular_ruler.primary_title
			set_color_from_title = scope:secular_ruler.primary_title
			set_capital_county = scope:secular_ruler.capital_county
			set_landless_title = yes
			set_no_automatic_claims = yes

			change_title_holder = {
				holder = scope:new_ecclesiastical_holder
				change = scope:change
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
	scope:actor = {
		if = {
			limit = {
				any_character_situation = {
					situation_type = the_christian_church
					situation_top_has_catalyst = catalyst_the_christian_church_clerical_region_split
				}
			}
			situation:the_christian_church = {
				trigger_situation_catalyst = catalyst_the_christian_church_clerical_region_split
			}
		}
		hidden_effect = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = split_ecclesiastical_title_toast
				left_icon = scope:actor
				right_icon = scope:new_ecclesiastical_holder
				custom_tooltip = {
					text = split_ecclesiastical_title_effect_tt
				}
			}
		}
		reverse_add_opinion = {
			target = scope:new_ecclesiastical_holder
			modifier = grateful_opinion
			opinion = 50
		}
		reverse_add_opinion = {
			target = scope:secondary_actor
			modifier = pleased_opinion
			opinion = 25
		}
		if = {
			# On a self-split the HoF reorganises their own region, so there is no aggrieved superior to anger
			limit = { scope:recipient != scope:actor }
			reverse_add_opinion = {
				target = scope:recipient
				modifier = angry_opinion
				opinion = -50
			}
		}
		add_hook = {
			type = favor_hook
			target = scope:new_ecclesiastical_holder
		}
	}
	if = {
		# Skip the "your region was split" notification when the HoF split their own region
		limit = { scope:recipient != scope:actor }
		scope:recipient = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = split_ecclesiastical_title_toast
				left_icon = scope:actor
				right_icon = scope:new_ecclesiastical_holder
				custom_tooltip = split_ecclesiastical_title_effect_tt
			}
		}
	}
	scope:new_ecclesiastical_holder = {
		if = {
			limit = {
				NOT = { government_has_flag = government_is_ecclesiastical }
			}
			change_government = ecclesiastical_government
		}
	}
}

# Merge an Ecclesiastical Title as HoF - on_accept
pam_merge_new_ecclesiastical_title_interaction_effect = {
	$ABSORBING_TITLE$ ?= {
		save_scope_as = absorbing_title
		holder ?= { save_scope_as = absorbing_holder }
	}
	$ABSORBED_TITLE$ = {
		save_scope_as = absorbed_title
		holder = {
			save_scope_as = absorbed_holder
			scope:absorbed_holder = {
				send_interface_toast = {
					type = event_toast_effect_bad
					title = request_ecclesiastical_title_merger_absorbed_tt
					left_icon = scope:absorbing_holder
					right_icon = scope:absorbed_title
					if = {
						limit = { is_ai = yes }
						# Absorbed region holder is angry at the initiator
						add_opinion = {
							target = $SENDER$
							modifier = angry_opinion
							opinion = -75
						}
						progress_towards_rival_effect = {
							CHARACTER = $SENDER$
							OPINION = 0
							REASON = rival_ecclesiastical_title_merged
						}
					}
				}
			}
		}
	}
	# Toast to the initiator
	$SENDER$ = {
		hidden_effect = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = merge_new_ecclesiastical_title_toast
				left_icon = scope:absorbing_holder
				right_icon = scope:absorbed_title
				# Perform the region merge
				custom_tooltip = {
					text = merge_new_ecclesiastical_title_effect_tt
					scope:absorbing_title = {
						grow_clerical_region_from = scope:absorbed_title
						set_variable = {
							name = merge_clerical_region_cooldown
							years = merge_clerical_region_cooldown_years_value
						}
					}
				}
			}
		}
	}
	scope:absorbing_holder = {
		send_interface_toast = {
			type = event_toast_effect_good
			title = merge_new_ecclesiastical_title_toast
			left_icon = $SENDER$
			right_icon = scope:absorbed_title
			custom_tooltip = merge_new_ecclesiastical_title_effect_tt
			# The puppet variant sends on its own behalf, so there is no favour owed.
			if = {
				limit = { NOT = { this = $SENDER$ } }
				if = {
					limit = { is_ai = yes }
					add_opinion = {
						target = $SENDER$
						modifier = grateful_opinion
						opinion = 50
					}
				}
				$SENDER$ = {
					add_hook = {
						type = favor_hook
						target = scope:absorbing_holder
					}
				}
			}
		}
	}
}

pam_secure_pilgrim_routes_effect = {
	faith = {
		set_variable = {
			name = pam_secured_pilgrim_routes
			years = 25
		}
		main_rite = {
			remove_rite_tenet = scope:removed_tenet
			add_rite_tenet = tenet_armed_pilgrimages
		}
		# The removed tenet loses, and the new core tenet gains popularity.
		if = {
			limit = { has_tenet_popularity_trigger = yes }
			change_tenet_popularity = {
				target = scope:removed_tenet
				change = major_tenet_popularity_loss
			}
			change_tenet_popularity = {
				target = tenet:tenet_armed_pilgrimages
				change = major_tenet_popularity_gain
			}
		}
	}
	add_character_modifier = {
		modifier = pam_protector_of_pilgrims_modifier
		years = 25
	}
	custom_tooltip = pam_secure_pilgrim_routes_pilgrim_xp_tt
	custom_tooltip = pam_secure_pilgrim_routes_ai_pilgrimage_tt
}

spiritual_hof_or_realm_priest_superior_recipient_redirect_effect = {
	#redirect low tiers targeting HoF to archbishops
	if = {
		limit = {
			highest_held_title_tier < tier_kingdom
			faith.religious_head ?= scope:recipient
			OR = {
				exists = cp:councillor_court_chaplain.superior
				AND = {
					government_has_flag = government_is_theocracy
					exists = superior
				}
			}
		}
		#Actor is a priest themselves
		if = {
			limit = {
				AND = {
					government_has_flag = government_is_theocracy
					exists = superior
				}
			}
			superior = { save_scope_as = recipient }
		}
		#Actor is secular
		else = {
			cp:councillor_court_chaplain.superior = {
				save_scope_as = recipient
			}
		}
	}
	#redirect high tiers targeting archbishops to HoF
	else_if = {
		limit = {
			highest_held_title_tier >= tier_kingdom
			OR = {
				superior ?= scope:recipient
				cp:councillor_court_chaplain.superior ?= scope:recipient
			}
			faith = {
				has_doctrine = doctrine_spiritual_head
				religious_head ?= {
					NOR = {
						this = scope:recipient
						this = scope:puppet_or_actor
					}
				}
			}
		}
		faith.religious_head = {
			save_scope_as = recipient
		}
	}
}
non_pope_lifting_excommunication_penalty_effect = {
	if = {
		limit = {
			scope:puppet_or_actor = {
				has_character_flag = pope_excom
			}
			NOT = {
				scope:puppet_or_actor.faith.religious_head ?= scope:recipient
			}
		}
		scope:recipient = {
			add_piety = massive_piety_loss
			add_piety_level = -2
		}
		scope:puppet_or_actor.faith.religious_head = {
			add_opinion = {
				target = scope:recipient
				opinion = -80
				modifier = betrayal_opinion
			}
		}
	}
}

seek_indulgences_refusal_effect = {
	scope:recipient = {
		add_prestige = {
			value = minor_prestige_value
			multiply = {
				value = scope:actor.highest_held_title_tier
				subtract = 1
			}
		}
	}
	scope:actor = {
		clerical_interaction_refusal_minor_effect = yes
		if = {
			limit = {
				scope:recipient = {
					is_ai = yes
				}
			}
			if = {
				limit = {
					scope:actor = {
						is_ai = yes
					}
				} 
				add_character_modifier = {
					modifier = pam_refused_indulgences_modifier_small
					years = 5
					desc = refused_indulgences_effect_tt
				}
				custom_tooltip = refused_indulgences_ai_personality_effect_tt
				custom_tooltip = refused_indulgences_ai_war_effect_tt
				custom_tooltip = refused_indulgences_ai_amenity_effect_tt
			}
			else = {
				add_character_modifier = {
					modifier = pam_refused_indulgences_modifier_small
					years = 5
				}
			}
			change_spiritual_fulfillment = minor_spiritual_fulfillment_loss
		}
		else = {
			change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
			if = {
				limit = {
					scope:actor = {
						is_ai = yes
					}
				}
				add_character_modifier = {
					modifier = pam_refused_indulgences_modifier
					years = 5
					desc = refused_indulgences_effect_tt
				}
				custom_tooltip = refused_indulgences_ai_personality_effect_tt
				custom_tooltip = refused_indulgences_ai_war_effect_tt
				custom_tooltip = refused_indulgences_ai_amenity_effect_tt
			}
			else = {
				add_character_modifier = {
					modifier = pam_refused_indulgences_modifier
					years = 5
				}
			}
		}
	}
}
divorce_house_feud_effect = {
	house_feud_start_effect = {
		# Feuding House Head
		ACTOR = scope:divorcer.house.house_head
		# Target House Head
		TARGET = scope:secondary_recipient.house.house_head
		# Feud Reason
		REASON = divorce
		# House Member attacker if relevant 
		ATTACKER = scope:divorcer
		# House Member victim if relevant
		VICTIM = scope:secondary_recipient
	}
	scope:secondary_recipient = {
		add_prestige_level = -1
	}
}
divorce_holy_order_lease_effect = {
	scope:barony_to_lease = { lease_out_to_holy_order = scope:lease_order }
}
divorce_get_hook_effect = {
	if = {
		limit = {
			exists = scope:forcing_divorce
		}
		add_hook = {
			type = favor_hook
			target = scope:forcing_divorce
		}
	}
	else = {
		add_hook = {
			type = favor_hook
			target = scope:divorcer
		}
	}
	
}
divorce_put_spouse_in_monastery_effect = {
	scope:secondary_recipient = {
		add_trait = devoted
		scope:recipient = {
			add_courtier = scope:secondary_recipient
		}
	}
}

#############################
# Tenet popularity          #
#############################

# on_personal_tenet_gain fires this, with root as the character and scope:tenet the gained tenet
# Popularity gain is the lowest possible value, scaled by title tier
pam_tenet_popularity_on_personal_tenet_gain_effect = {
	save_scope_as = tp_personal_tenet_changer
	faith = {
		if = {
			limit = { has_tenet_popularity_trigger = yes }
			change_tenet_popularity = {
				target = scope:tenet
				change = scope:tp_personal_tenet_changer.pam_personal_tenet_popularity_gain
			}
		}
	}
}

# on_personal_tenet_loss runs this with root as the character and scope:tenet as the dropped tenet
pam_tenet_popularity_on_personal_tenet_loss_effect = {
	save_scope_as = tp_personal_tenet_changer
	faith = {
		if = {
			limit = { has_tenet_popularity_trigger = yes }
			change_tenet_popularity = {
				target = scope:tenet
				change = scope:tp_personal_tenet_changer.pam_personal_tenet_popularity_loss
			}
		}
	}
}

# The newly elected pope is root
pam_tenet_popularity_on_new_pope_effect = {
	if = {
		limit = { faith = { has_tenet_popularity_trigger = yes } }
		faith = { save_scope_as = tp_new_pope_faith }
		every_personal_tenet = {
			save_scope_as = tp_new_pope_tenet
			scope:tp_new_pope_faith = {
				change_tenet_popularity = {
					target = scope:tp_new_pope_tenet
					change = medium_tenet_popularity_gain
				}
			}
		}
		rite = {
			every_rite_tenet = {
				status = core
				save_scope_as = tp_new_pope_tenet
				scope:tp_new_pope_faith = {
					change_tenet_popularity = {
						target = scope:tp_new_pope_tenet
						change = medium_tenet_popularity_gain
					}
				}
			}
		}
	}
}

pam_set_pope_rite_mainline_effect = {
	if = {
		limit = {
			exists = rite
			rite != faith.main_rite
		}
		rite = {
			set_main_rite = yes
		}
	}
}

pam_pope_core_tenet_popularity_catalyst_effect = {
	if = {
		limit = {
			has_pam_dlc_trigger = yes
			exists = scope:tenet_to_add
			religion = religion:christianity_religion
			exists = situation:the_christian_church
		}
		if = {
			limit = {
				"faith.tenet_popularity(scope:tenet_to_add)" >= tenet_popularity_popular_threshold
				situation:the_christian_church = {
					situation_top_has_catalyst = catalyst_pope_declared_popular_core_tenet
				}
			}
			situation:the_christian_church = {
				trigger_situation_catalyst = {
					catalyst = catalyst_pope_declared_popular_core_tenet
					character = root
				}
			}
		}
		else_if = {
			limit = {
				"faith.tenet_popularity(scope:tenet_to_add)" < tenet_popularity_popular_threshold
				situation:the_christian_church = {
					situation_top_has_catalyst = catalyst_pope_declared_unpopular_core_tenet
				}
			}
			situation:the_christian_church = {
				trigger_situation_catalyst = {
					catalyst = catalyst_pope_declared_unpopular_core_tenet
					character = root
				}
			}
		}
	}
}

# Head of Faith enacts a papal bull. Called from the after block of each bull event, where root is the issuer.
pam_the_christian_church_papal_bull_enacted_catalyst_effect = {
	if = {
		limit = {
			has_pam_dlc_trigger = yes
			religion = religion:christianity_religion
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_the_christian_church_papal_bull_enacted
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = {
				catalyst = catalyst_the_christian_church_papal_bull_enacted
				character = root
			}
		}
	}
}

pam_react_to_new_pope_effect = {
	if = {
		limit = {
			has_pam_dlc_trigger = yes
			current_date_is_start_date_trigger = no
			is_alive = yes
			is_ruler = yes
		}
		save_scope_as = new_pope
		faith = {
			every_faith_character = {
				limit = {
					is_ruler = yes
					this != scope:new_pope
				}
				hidden_effect = { remove_character_modifier = pam_denied_new_pope_modifier }
				if = {
					limit = { has_variable = pam_new_pope_events_as_feed_message }
					send_interface_message = {
						type = msg_pam_new_pope_elected
						title = {
							first_valid = {
								triggered_desc = {
									trigger = { scope:new_pope = { is_female = yes } }
									desc = pam_clergy_events.0030.t.female
								}
								desc = pam_clergy_events.0030.t
							}
						}
						desc = pam_clergy_events.0030.feed
						left_icon = scope:new_pope
					}
				}
				else = {
					trigger_event = pam_clergy_events.0030
				}
			}
		}
	}
}

# Root = the kinsman who just took the office. $TITLE$ = the office title, $KIND$ = flag:head_of_faith / flag:antipope / flag:archbishop
pam_family_becomes_prelate_effect = {
	if = {
		limit = {
			has_pam_dlc_trigger = yes
			current_date_is_start_date_trigger = no
			is_alive = yes
			religion = religion:christianity_religion
			# Every string in 0060 names the dynasty, so a lowborn prelate has no story to tell here
			exists = dynasty
		}
		save_scope_as = new_prelate
		$TITLE$ = { save_scope_as = prelate_title }
		save_scope_value_as = {
			name = prelate_office
			value = $KIND$
		}
		every_player = {
			limit = {
				this != scope:new_prelate
				OR = {
					is_close_or_extended_family_of = scope:new_prelate
					house ?= scope:new_prelate.house
				}
				# Archdioceses change hands far more often than the great offices do
				trigger_if = {
					limit = { scope:prelate_office = flag:archbishop }
					NOT = { has_character_flag = pam_family_prelate_cooldown }
				}
			}
			if = {
				limit = { scope:prelate_office = flag:archbishop }
				add_character_flag = {
					flag = pam_family_prelate_cooldown
					years = 10
				}
			}
			trigger_event = {
				id = pam_clergy_events.0060
				days = { 3 5 }
			}
		}
	}
}

# Root = the new archbishop. $TITLE$ = the clerical region title that changed hands.
pam_react_to_new_archbishop_effect = {
	if = {
		limit = {
			has_pam_dlc_trigger = yes
			current_date_is_start_date_trigger = no
			is_alive = yes
		}
		save_scope_as = new_archbishop
		$TITLE$ = { save_scope_as = archdiocese }
		every_player = {
			limit = {
				this != scope:new_archbishop
				is_ruler = yes
				# The clerical_region_title is geographic, so a non-Christian holder would be told they answer to it
				religion = religion:christianity_religion
				capital_county.clerical_region_title ?= scope:archdiocese
			}
			send_interface_message = {
				type = msg_pam_new_archbishop
				left_icon = scope:new_archbishop
				right_icon = scope:archdiocese
			}
		}
	}
}

pam_tenet_popularity_on_evangelized_tenet_effect = {
	save_temporary_scope_as = pam_evangelizer
	faith = {
		if = {
			limit = { has_tenet_popularity_trigger = yes }
			change_tenet_popularity = {
				target = $TENET$
				change = {
					value = $CHANGE$
					# A Faithful Flock dynasty legacy: evangelizing dynasty members spread their tenets further
					if = {
						limit = {
							scope:pam_evangelizer = {
								dynasty ?= { has_dynasty_perk = pam_legacy_3 }
							}
						}
						multiply = 1.5
					}
				}
			}
		}
	}
}

# Root = preacher, $TENET$ = the tenet
# Once per preacher per activity, and we keep the record on the activity so it cannot leak
pam_spread_tenet_flat_popularity_once_effect = {
	save_temporary_scope_as = pam_spread_flat_preacher
	if = {
		limit = {
			involved_activity ?= {
				NAND = {
					has_variable_list = pam_spread_tenet_flat_paid
					is_target_in_variable_list = {
						name = pam_spread_tenet_flat_paid
						target = scope:pam_spread_flat_preacher
					}
				}
			}
		}
		involved_activity = {
			add_to_variable_list = {
				name = pam_spread_tenet_flat_paid
				target = scope:pam_spread_flat_preacher
			}
		}
		pam_tenet_popularity_on_evangelized_tenet_effect = {
			TENET = $TENET$
			CHANGE = medium_tenet_popularity_gain
		}
	}
}

# A converting character is root, scope:old_rite the previous rite
pam_tenet_popularity_on_rite_change_effect = {
	if = {
		limit = { is_landed = yes }
		faith = { save_scope_as = tp_new_faith }
		rite = { save_scope_as = tp_new_rite }
		if = {
			limit = {
				exists = scope:old_rite
				scope:old_rite != scope:tp_new_rite
			}
			scope:old_rite.faith = { save_scope_as = tp_old_faith }
			if = {
				limit = { scope:tp_old_faith = { has_tenet_popularity_trigger = yes } }
				scope:old_rite = {
					every_rite_tenet = {
						status = core
						save_scope_as = tp_tenet
						scope:tp_old_faith = {
							change_tenet_popularity = {
								target = scope:tp_tenet
								change = miniscule_tenet_popularity_loss
							}
						}
					}
				}
			}
		}
		if = {
			limit = { scope:tp_new_faith = { has_tenet_popularity_trigger = yes } }
			scope:tp_new_rite = {
				every_rite_tenet = {
					status = core
					save_scope_as = tp_tenet
					scope:tp_new_faith = {
						change_tenet_popularity = {
							target = scope:tp_tenet
							change = miniscule_tenet_popularity_gain
						}
					}
				}
			}
		}
	}
}

# on_county_faith_change sets root as the county and scope:old_faith as the previous faith
# var:tp_popularity_rite remembers the last credited rite so the old faith can be uncredited
pam_tenet_popularity_on_county_faith_change_effect = {
	faith = { save_scope_as = tp_new_faith }
	rite = { save_scope_as = tp_new_rite }
	if = {
		limit = { exists = var:tp_popularity_rite }
		var:tp_popularity_rite = { save_scope_as = tp_old_rite }
		if = {
			limit = { scope:old_faith = { has_tenet_popularity_trigger = yes } }
			scope:tp_old_rite = {
				every_rite_tenet = {
					status = core
					save_scope_as = tp_tenet
					scope:old_faith = {
						change_tenet_popularity = {
							target = scope:tp_tenet
							change = miniscule_tenet_popularity_loss
						}
					}
				}
			}
		}
	}
	if = {
		limit = { scope:tp_new_faith = { has_tenet_popularity_trigger = yes } }
		scope:tp_new_rite = {
			every_rite_tenet = {
				status = core
				save_scope_as = tp_tenet
				scope:tp_new_faith = {
					change_tenet_popularity = {
						target = scope:tp_tenet
						change = miniscule_tenet_popularity_gain
					}
				}
			}
		}
	}
	set_variable = {
		name = tp_popularity_rite
		value = rite
	}
}

# Study Rite: scope:discovered_tenet learned by root (a character).
pam_study_rite_tenet_popularity_effect = {
	if = {
		limit = {
			exists = scope:discovered_tenet
			faith = { has_tenet_popularity_trigger = yes }
		}
		faith = {
			change_tenet_popularity = {
				target = scope:discovered_tenet
				change = minor_tenet_popularity_gain
			}
		}
	}
}

# on_rite_created. scope:new_rite must exist; root is founder.
pam_tenet_popularity_on_rite_created_effect = {
	if = {
		limit = {
			scope:new_rite.faith ?= { has_tenet_popularity_trigger = yes }
		}
		root = { save_scope_as = rite_founder }
		scope:new_rite = {
			save_scope_as = created_rite
			every_rite_tenet = {
				status = core
				save_scope_as = tenet
				scope:created_rite.faith = {
					change_tenet_popularity = {
						target = scope:tenet
						change = scope:created_rite.tenet_popularity_rite_created_gain
					}
				}
			}
		}
	}
}

# Bump one tenet across all Christian faiths with map presence.
pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
	religion:christianity_religion = {
		every_faith = {
			limit = {
				num_county_followers >= 1
				has_tenet_popularity_trigger = yes
			}
			change_tenet_popularity = {
				target = tenet:$TENET$
				change = $VALUE$
			}
		}
	}
}

# Situation phase on_start hooks (The Christian Church).
pam_tenet_popularity_situation_fragile_unity_start_effect = {
	pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
		TENET = tenet_peace_of_god
		VALUE = miniscule_tenet_popularity_gain
	}
}

pam_tenet_popularity_situation_saeculum_obscurum_start_effect = {
	pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
		TENET = tenet_simony
		VALUE = medium_tenet_popularity_gain
	}
}

pam_tenet_popularity_situation_conversion_phase_start_effect = {
	pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
		TENET = tenet_ora_et_labora
		VALUE = medium_tenet_popularity_gain
	}
}

pam_tenet_popularity_situation_reform_phase_start_effect = {
	pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
		TENET = tenet_ora_et_labora
		VALUE = medium_tenet_popularity_gain
	}
	pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
		TENET = tenet_simony
		VALUE = medium_tenet_popularity_loss
	}
}

pam_tenet_popularity_situation_investiture_crisis_start_effect = {
	pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
		TENET = tenet_apostolic_succession
		VALUE = medium_tenet_popularity_loss
	}
}

pam_tenet_popularity_situation_crusade_phase_start_effect = {
	pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
		TENET = tenet_miles_christi
		VALUE = medium_tenet_popularity_gain
	}
	pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
		TENET = tenet_armed_pilgrimages
		VALUE = medium_tenet_popularity_gain
	}
}

pam_tenet_popularity_situation_concord_start_effect = {
	pam_tenet_popularity_christian_faiths_tenet_delta_effect = {
		TENET = tenet_love_thy_neighbour
		VALUE = medium_tenet_popularity_gain
	}
}

# Without this, clerical courts fill from generic pool templates and feel secular
pam_seed_monk_courtiers_effect = {
	if = {
		limit = { pam_court_wants_monk_courtiers_trigger = yes }
		save_temporary_scope_as = monk_employer
		while = {
			count = 4 # Hard bound matching the highest quota; the limit below is what actually ends the loop
			limit = {
				any_courtier = {
					count < pam_monk_courtier_target_value
					OR = {
						has_trait = devoted
						has_trait = cleric
					}
				}
			}
			random_list = {
				50 = {
					create_character = { # No location: create_character rejects location and employer together
						employer = scope:monk_employer
						template = pam_monk_courtier_template
					}
				}
				50 = {
					create_character = {
						employer = scope:monk_employer
						template = pam_cleric_courtier_template
					}
				}
			}
		}
	}
}

# Game start only, because if the three-year pulse ran this the split would creep toward all courts
pam_seed_nun_courtier_effect = {
	if = {
		limit = {
			pam_court_wants_monk_courtiers_trigger = yes
			NOT = {
				any_courtier = {
					is_female = yes
					has_trait = devoted
				}
			}
		}
		save_temporary_scope_as = monk_employer
		random = {
			chance = 50
			create_character = {
				employer = scope:monk_employer
				template = pam_nun_courtier_template
			}
		}
	}
}

# Here we run this on the courtier, and it expects scope:pam_clerical_patron
pam_clerical_court_ordain_effect = {
	save_scope_as = pam_clerical_ordained
	scope:pam_clerical_patron = {
		send_interface_toast = {
			type = event_toast_effect_good
			title = pam_clerical_court_ordain_toast
			left_icon = scope:pam_clerical_ordained
			scope:pam_clerical_ordained = {
				add_trait = cleric
				if = {
					limit = { learning < scope:pam_clerical_patron.pam_clerical_court_learning_cap_value }
					add_learning_skill = pam_clerical_court_learning_gain_value
				}
				add_opinion = {
					target = scope:pam_clerical_patron
					modifier = grateful_for_calling_opinion
				}
			}
			add_piety = pam_clerical_court_piety_gain_value
		}
	}
}

# scope:pam_clerical_patron is required here, and we run this on the courtier
pam_clerical_court_instruct_effect = {
	save_scope_as = pam_clerical_instructed
	scope:pam_clerical_patron = {
		send_interface_toast = {
			type = event_toast_effect_good
			title = pam_clerical_court_instruct_toast
			left_icon = scope:pam_clerical_instructed
			scope:pam_clerical_instructed = {
				if = {
					limit = { learning < scope:pam_clerical_patron.pam_clerical_court_learning_cap_value }
					add_learning_skill = pam_clerical_court_learning_gain_value
				}
				if = {
					limit = { NOT = { has_trait = lifestyle_scholar } }
					add_trait = lifestyle_scholar
				}
				else_if = {
					limit = {
						has_trait_xp = { trait = lifestyle_scholar value < 100 }
					}
					add_trait_xp = {
						trait = lifestyle_scholar
						value = 20
					}
				}
				else_if = {
					limit = { NOT = { has_trait = lifestyle_mystic } }
					add_trait = lifestyle_mystic
				}
				else_if = {
					limit = {
						has_trait_xp = { trait = lifestyle_mystic value < 100 }
					}
					add_trait_xp = {
						trait = lifestyle_mystic
						value = 25
					}
				}
			}
		}
	}
}

# Recruiting rulers run this, and it expects scope:pam_clerical_patron
pam_clerical_court_recruit_effect = {
	if = {
		limit = { exists = scope:pam_clerical_novice_current }
		clear_saved_scope = pam_clerical_novice_current
	}
	if = {
		limit = {
			exists = scope:pam_clerical_novice
			# Can cease to be in the pool between the event firing and the option executing
			scope:pam_clerical_novice = { is_pool_character = yes }
		}
		scope:pam_clerical_novice = { save_scope_as = pam_clerical_novice_current }
	}
	else_if = {
		limit = {
			any_pool_character = {
				province = scope:pam_clerical_patron.location
				pam_clerical_court_novice_trigger = { RULER = scope:pam_clerical_patron }
				faith = scope:pam_clerical_patron.faith
			}
		}
		random_pool_character = {
			province = scope:pam_clerical_patron.location
			limit = {
				pam_clerical_court_novice_trigger = { RULER = scope:pam_clerical_patron }
				faith = scope:pam_clerical_patron.faith
			}
			save_scope_as = pam_clerical_novice_current
		}
	}
	else_if = {
		limit = {
			any_pool_character = {
				province = scope:pam_clerical_patron.location
				pam_clerical_court_novice_trigger = { RULER = scope:pam_clerical_patron }
			}
		}
		random_pool_character = {
			province = scope:pam_clerical_patron.location
			limit = { pam_clerical_court_novice_trigger = { RULER = scope:pam_clerical_patron } }
			save_scope_as = pam_clerical_novice_current
		}
	}
	if = {
		limit = { exists = scope:pam_clerical_novice_current }
		add_courtier = scope:pam_clerical_novice_current
		send_interface_toast = {
			type = event_toast_effect_good
			title = pam_clerical_court_recruit_toast
			left_icon = scope:pam_clerical_novice_current
			if = {
				limit = {
					NOT = { scope:pam_clerical_novice_current.faith = scope:pam_clerical_patron.faith }
				}
				scope:pam_clerical_novice_current = {
					set_character_faith = scope:pam_clerical_patron.faith
				}
			}
			if = {
				limit = { scope:pam_clerical_novice_current = { NOT = { rite = scope:pam_clerical_patron.rite } } }
				scope:pam_clerical_novice_current = {
					set_character_rite = scope:pam_clerical_patron.rite
				}
			}
			if = {
				limit = { scope:pam_clerical_novice_current = { NOT = { has_trait = cleric } } }
				scope:pam_clerical_novice_current = {
					add_trait = cleric
				}
			}
		}
	}
}

# At game start we leave prohibited tenets at zero
pam_tenet_popularity_on_game_start_effect = {
	if = {
		limit = { has_pam_dlc_trigger = yes }
		religion:christianity_religion = {
			every_faith = {
				limit = { num_county_followers >= 1 }
				save_scope_as = tp_faith
				main_rite = { save_scope_as = tp_rite }
				# Any follower works as the host scope for the tenet iterator; rite_filter drives the list.
				random_faith_county = {
					limit = { exists = holder }
					holder = {
						pam_tenet_popularity_seed_status_effect = { RITE = scope:tp_rite FAITH = scope:tp_faith STATUS = core VALUE = massive_tenet_popularity_value }
						pam_tenet_popularity_seed_status_effect = { RITE = scope:tp_rite FAITH = scope:tp_faith STATUS = permitted VALUE = major_tenet_popularity_value }
						pam_tenet_popularity_seed_status_effect = { RITE = scope:tp_rite FAITH = scope:tp_faith STATUS = known VALUE = medium_tenet_popularity_value }
					}
				}
			}
		}
	}
}

# $RITE$ is the rite scope, run after a title adopts a new state rite
pam_tenet_popularity_boost_rite_effect = {
	$RITE$ = {
		if = {
			limit = {
				has_pam_dlc_trigger = yes
				faith.religion = religion:christianity_religion
				faith = { num_county_followers >= 1 }
			}
			save_scope_as = tp_boost_rite
			faith = {
				save_scope_as = tp_boost_faith
				# Any follower works as the host scope for the tenet iterator; rite_filter drives the list.
				random_faith_county = {
					limit = { exists = holder }
					holder = {
						pam_tenet_popularity_seed_status_effect = { RITE = scope:tp_boost_rite FAITH = scope:tp_boost_faith STATUS = core VALUE = massive_tenet_popularity_value }
						pam_tenet_popularity_seed_status_effect = { RITE = scope:tp_boost_rite FAITH = scope:tp_boost_faith STATUS = permitted VALUE = major_tenet_popularity_value }
						pam_tenet_popularity_seed_status_effect = { RITE = scope:tp_boost_rite FAITH = scope:tp_boost_faith STATUS = known VALUE = medium_tenet_popularity_value }
					}
				}
			}
		}
	}
}

# Credits every $RITE$ tenet at $STATUS$ with $VALUE$ popularity for $FAITH$
# Root is only a host for the tenet iterator, and its own knowledge is ignored
pam_tenet_popularity_seed_status_effect = {
	if = {
		limit = { $FAITH$ = { has_tenet_popularity_trigger = yes } }
		every_character_tenet = {
			rite_filter = $RITE$
			status = $STATUS$
			save_scope_as = tp_seed_tenet
			$FAITH$ = {
				change_tenet_popularity = {
					target = scope:tp_seed_tenet
					change = $VALUE$
				}
			}
		}
	}
}

pam_save_selected_interaction_tenet_effect = {
	# Set the tenet based on selected send_options
	switch = {
		trigger = yes
		scope:tenet_aniconism = {
			tenet:tenet_aniconism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_alexandrian_catechism = {
			tenet:tenet_alexandrian_catechism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_armed_pilgrimages = {
			tenet:tenet_armed_pilgrimages = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_carnal_exaltation = {
			tenet:tenet_carnal_exaltation = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_communal_identity = {
			tenet:tenet_communal_identity = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_communion = {
			tenet:tenet_communion = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_consolamentum = {
			tenet:tenet_consolamentum = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_divine_marriage = {
			tenet:tenet_divine_marriage = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_gnosticism = {
			tenet:tenet_gnosticism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_mendicant_preachers = {
			tenet:tenet_mendicant_preachers = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_pacifism = {
			tenet:tenet_pacifism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_pentarchy = {
			tenet:tenet_pentarchy = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_unrelenting_faith = {
			tenet:tenet_unrelenting_faith = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_vows_of_poverty = {
			tenet:tenet_vows_of_poverty = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_pastoral_isolation = {
			tenet:tenet_pastoral_isolation = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_adaptive = {
			tenet:tenet_adaptive = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_esotericism = {
			tenet:tenet_esotericism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_legalism = {
			tenet:tenet_legalism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_literalism = {
			tenet:tenet_literalism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_reincarnation = {
			tenet:tenet_reincarnation = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_religious_legal_pronouncements = {
			tenet:tenet_religious_legal_pronouncements = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_struggle_submission = {
			tenet:tenet_struggle_submission = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_false_conversion_sanction = {
			tenet:tenet_false_conversion_sanction = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_tax_nonbelievers = {
			tenet:tenet_tax_nonbelievers = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_asceticism = {
			tenet:tenet_asceticism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_bhakti = {
			tenet:tenet_bhakti = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_dharmic_pacifism = {
			tenet:tenet_dharmic_pacifism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_inner_journey = {
			tenet:tenet_inner_journey = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_ritual_hospitality = {
			tenet:tenet_ritual_hospitality = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_adorcism = {
			tenet:tenet_adorcism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_ancestor_worship = {
			tenet:tenet_ancestor_worship = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_astrology = {
			tenet:tenet_astrology = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_hedonistic = {
			tenet:tenet_hedonistic = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_human_sacrifice = {
			tenet:tenet_human_sacrifice = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_mystical_birthright = {
			tenet:tenet_mystical_birthright = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_ritual_celebrations = {
			tenet:tenet_ritual_celebrations = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_sacred_childbirth = {
			tenet:tenet_sacred_childbirth = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_sanctity_of_nature = {
			tenet:tenet_sanctity_of_nature = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_sun_worship = {
			tenet:tenet_sun_worship = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_warmonger = {
			tenet:tenet_warmonger = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_gruesome_festivals = {
			tenet:tenet_gruesome_festivals = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_cthonic_redoubts = {
			tenet:tenet_cthonic_redoubts = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_household_gods = {
			tenet:tenet_household_gods = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_sinitic_syncretism = {
			tenet:tenet_sinitic_syncretism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_eastern_syncretism = {
			tenet:tenet_eastern_syncretism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_unreformed_syncretism = {
			tenet:tenet_unreformed_syncretism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_christian_syncretism = {
			tenet:tenet_christian_syncretism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_islamic_syncretism = {
			tenet:tenet_islamic_syncretism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_jewish_syncretism = {
			tenet:tenet_jewish_syncretism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_exaltation_of_pain = {
			tenet:tenet_exaltation_of_pain = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_natural_primitivism = {
			tenet:tenet_natural_primitivism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_pursuit_of_power = {
			tenet:tenet_pursuit_of_power = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_ritual_cannibalism = {
			tenet:tenet_ritual_cannibalism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_sacred_shadows = {
			tenet:tenet_sacred_shadows = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_polyamory = {
			tenet:tenet_polyamory = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_sacrificial_ceremonies = {
			tenet:tenet_sacrificial_ceremonies = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_megaliths = {
			tenet:tenet_megaliths = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_fp3_fedayeen = {
			tenet:tenet_fp3_fedayeen = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_communal_possessions = {
			tenet:tenet_communal_possessions = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_pure_land = {
			tenet:tenet_pure_land = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_no_mind = {
			tenet:tenet_no_mind = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_mountain_worship = {
			tenet:tenet_mountain_worship = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_extinction_of_dharma = {
			tenet:tenet_extinction_of_dharma = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_cranial_trophies = {
			tenet:tenet_cranial_trophies = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_pursuit_of_knowledge = {
			tenet:tenet_pursuit_of_knowledge = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_benevolent_governance = {
			tenet:tenet_benevolent_governance = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_filial_piety = {
			tenet:tenet_filial_piety = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_harmonious_society = {
			tenet:tenet_harmonious_society = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_sacred_destruction = {
			tenet:tenet_sacred_destruction = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_preservation = {
			tenet:tenet_preservation = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_takamin = {
			tenet:tenet_takamin = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_peace_of_god = {
			tenet:tenet_peace_of_god = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_dulia = {
			tenet:tenet_dulia = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_transubstantiation = {
			tenet:tenet_transubstantiation = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_ora_et_labora = {
			tenet:tenet_ora_et_labora = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_hyperdulia = {
			tenet:tenet_hyperdulia = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_simony = {
			tenet:tenet_simony = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_monophysitism = {
			tenet:tenet_monophysitism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_acts_of_apostles = {
			tenet:tenet_acts_of_apostles = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_purgatory = {
			tenet:tenet_purgatory = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_be_fruitful_and_multiply = {
			tenet:tenet_be_fruitful_and_multiply = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_apostolic_succession = {
			tenet:tenet_apostolic_succession = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_confession = {
			tenet:tenet_confession = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_original_sin = {
			tenet:tenet_original_sin = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_celestial_hierarchy = {
			tenet:tenet_celestial_hierarchy = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_miles_christi = {
			tenet:tenet_miles_christi = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_philanthropia = {
			tenet:tenet_philanthropia = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_scholasticism = {
			tenet:tenet_scholasticism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_adoptionism = {
			tenet:tenet_adoptionism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_love_thy_neighbour = {
			tenet:tenet_love_thy_neighbour = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_divine_liturgy = {
			tenet:tenet_divine_liturgy = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_miaphysitism = {
			tenet:tenet_miaphysitism = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_hesychasm = {
			tenet:tenet_hesychasm = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_anachoresis = {
			tenet:tenet_anachoresis = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_mortification_of_the_flesh = {
			tenet:tenet_mortification_of_the_flesh = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_holy_myron = {
			tenet:tenet_holy_myron = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
		scope:tenet_khachkar = {
			tenet:tenet_khachkar = {
				save_scope_as = pam_selected_interaction_tenet
			}
		}
	}
}

# Root is character that will have their personal tenet replaced with TENET
pam_replace_personal_tenet_effect = {
	save_temporary_scope_as = tenet_char
	if = {
		limit = {
			free_personal_tenet_slots = 0
		}
		random_personal_tenet = {
			weight = {
				base = 10
				modifier = {
					add = pam_tenet_popularity_impact_value
				}
			}
			scope:tenet_char = {
				remove_personal_tenet = prev
			}
		}
	}
	set_personal_tenet = $TENET$
}

pam_notify_holy_war_instigator_effect_win = {
	if = {
		limit = {
			scope:war ?= {
				has_variable = holy_instigator
				var:holy_instigator = {
					is_alive = yes
					save_temporary_scope_as = instigator_temp
				}
				NOT = {
					any_war_participant = {
						this = scope:instigator_temp
					}
				}
			}
		}
		every_in_list = {
			list = target_titles
			save_scope_as = target
		}
		
		scope:war = {
			var:holy_instigator = {
				save_scope_as = cleric
				send_interface_message = {
					type = event_generic_good
					title = pam_instigated_holy_war_won.t
					left_icon = scope:attacker
					right_icon = scope:defender

					if = {
						limit = {
							is_ai = no
						}
						add_prestige = pam_holy_war_inciter_prestige_value
					}
					else = {
						hidden_effect = {
							add_prestige = pam_holy_war_inciter_prestige_value
						}
					}

					custom_tooltip = pam_instigated_holy_war_won_tt
					if = {
						limit = {
							pam_can_expand_clerical_region_after_war_trigger = { HEATHEN = scope:defender }
						}
						if = {
							limit = {
								any_in_list = {
									list = target_titles
									tier = tier_county
								}
							}
							every_in_list = {
								list = target_titles
								duchy = {
									scope:cleric = {
										add_to_variable_list = {
											name = clergy_expands
											target = prev
										}
									}
								}
							}
						}
						else_if = {
							limit = {
								any_in_list = {
									list = target_titles
									tier = tier_duchy
								}
							}
							every_in_list = {
								list = target_titles
								scope:cleric = {
									add_to_variable_list = {
										name = clergy_expands
										target = prev
									}
								}
							}
						}
						else = {
							every_in_list = {
								list = target_titles
								every_direct_de_jure_vassal_title = {
									limit = {
										any_de_jure_county = {
											holder = {
												target_is_same_character_or_above = scope:defender
											}
										}
									}
									scope:cleric = {
										add_to_variable_list = {
											name = clergy_expands
											target = prev
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
pam_notify_holy_war_instigator_effect_draw = {
	if = {
		limit = {
			scope:war ?= {
				has_variable = holy_instigator
				var:holy_instigator = {
					is_alive = yes
					save_temporary_scope_as = instigator_temp
				}
				NOT = {
					any_war_participant = {
						this = scope:instigator_temp
					}
				}
			}
		}
		every_in_list = {
			list = target_titles
			save_scope_as = target
		}
		scope:war = {
			var:holy_instigator = {
				send_interface_message = {
					type = event_generic_bad
					title = pam_instigated_holy_war_draw.t
					left_icon = scope:attacker
					right_icon = scope:defender
					custom_tooltip = pam_instigated_holy_war_draw_tt
				}
			}
		}
	}
}
pam_notify_holy_war_instigator_effect_loss = {
	if = {
		limit = {
			scope:war ?= {
				has_variable = holy_instigator
				var:holy_instigator = {
					is_alive = yes
					save_temporary_scope_as = instigator_temp
				}
				NOT = {
					any_war_participant = {
						this = scope:instigator_temp
					}
				}
			}
		}
		every_in_list = {
			list = target_titles
			save_scope_as = target
		}
		scope:war = {
			if = {
				limit = {
					has_variable = holy_joiner
				}
				var:holy_joiner = {
					send_interface_message = {
						type = event_generic_bad
						title = pam_instigated_holy_war_lost.t
						left_icon = scope:attacker
						right_icon = scope:defender

						add_prestige_level = -2
					}
				}
			}
			else = {
				var:holy_instigator = {
					send_interface_message = {
						type = event_generic_bad
						title = pam_instigated_holy_war_lost.t
						left_icon = scope:attacker
						right_icon = scope:defender

						custom_tooltip = pam_instigated_holy_war_lost_tt
					}
				}
			}
		}
	}
}
pam_incite_holy_war_effect = {
	scope:puppet_or_actor = {
		if = {
			limit = {
				top_participant_group:the_christian_church ?= {
					has_participant_group_parameter = the_christian_church_incite_holy_war_interaction
				}
				NOR = {
					has_trait = forgiving
					has_trait = compassionate
				}
			}
			change_spiritual_fulfillment = {
				value = miniscule_spiritual_fulfillment_value
				multiply = {
					value = scope:target.tier
					subtract = 1
				}
			}
		}
	}
	
	scope:secondary_recipient = {
		save_scope_as = sponsored_holy_war_attacker
		add_piety = {
			value = medium_piety_value
			multiply = {
				value = scope:target.tier
				subtract = 1
			}
		}
		if = {
			limit = {
				scope:puppet_or_actor = {
					top_participant_group:the_christian_church ?= {
						has_participant_group_parameter = the_christian_church_incite_holy_war_interaction
					}
				}
				NOR = {
					has_trait = forgiving
					has_trait = compassionate
				}
			}
			change_spiritual_fulfillment = {
				value = minor_spiritual_fulfillment_value
				multiply = {
					value = scope:target.tier
					subtract = 1
				}
			}
		}
	}
	scope:recipient = {
		save_scope_as = sponsored_holy_war_defender
		if = {
			limit = {
				NOT = {
					root ?= scope:secondary_recipient
				}
			}
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = incited_holy_war_opinion
				opinion = -50
			}
		}
	}
	if = {
		limit = {
			root ?= scope:secondary_recipient
		}
		custom_tooltip = interaction_holy_war_attacker_tt
		show_as_tooltip = {
			scope:recipient = {
				add_opinion = {
					modifier = declared_war
					target = scope:secondary_recipient
				}
			}
		}
	}
	else = {
		scope:recipient = {
			custom_tooltip = interaction_holy_war_attacked_tt
		}
	}
	
	scope:sponsored_holy_war_attacker = {
		add_character_flag = {
			flag = holy_war_incited
			days = 10
		}
		if = {
			limit = {
				scope:sponsored_holy_war_target.tier = tier_kingdom
			}
			hidden_effect = {
				start_war = {
					casus_belli = major_religious_war
					target = scope:sponsored_holy_war_defender
					target_title = scope:sponsored_holy_war_target
				}
			}
		}
		else_if = {
			limit = {
				scope:sponsored_holy_war_target.tier = tier_duchy
			}
			hidden_effect = {
				start_war = {
					casus_belli = religious_war
					target = scope:sponsored_holy_war_defender
					target_title = scope:sponsored_holy_war_target
				}
			}
		}
		else = {
			hidden_effect = {
				start_war = {
					casus_belli = minor_religious_war
					target = scope:sponsored_holy_war_defender
					target_title = scope:sponsored_holy_war_target
				}
			}
		}
		random_character_war = {
			limit = {
				OR = {
					using_cb = major_religious_war
					using_cb = religious_war
					using_cb = minor_religious_war
				}
				primary_defender = scope:recipient
			}
			save_scope_as = holy_war
			set_variable = {
				name = holy_instigator
				value = scope:puppet_or_actor
			}
		}
	}
	scope:puppet_or_actor = {
		custom_tooltip = inciter_gain_prestige_if_holy_war_won_tt
		if = {
			limit = {
				scope:fund_war ?= yes
			}
			pay_treasury_or_gold = {
				target = scope:secondary_recipient
				value = scope:puppet_or_actor.major_gold_value
			}
		}
		if = {
			limit = {
				scope:join_war ?= yes
			}
			custom_tooltip = holy_war_interaction_join_war_tt
			custom_tooltip = holy_war_cleric_loss_penalty_tt
			scope:holy_war ?= {
				hidden_effect = { add_attacker = scope:puppet_or_actor }
				set_variable = {
					name = holy_joiner
					value = scope:puppet_or_actor
				}
			}
		}
		if = {
			limit = {
				scope:use_hook ?= yes
				has_usable_hook = scope:secondary_recipient
			}
			use_hook = scope:secondary_recipient
		}
		if = {
			limit = {
				scope:rally_crusaders ?= yes
			}
			scope:secondary_recipient = {
				if = {
					limit = {
						highest_held_title_tier < scope:puppet_or_actor.highest_held_title_tier
						current_military_strength < scope:puppet_or_actor.current_military_strength
					}
					if = {
						limit = {
							is_ai = yes
						}
						add_character_modifier = {
							modifier = crusader_momentum_modifier_large
							years = 10
							desc = crusader_momentum_ai_behavior_large_tt
						}
						custom_tooltip = crusader_momentum_ai_behavior_large_tt
					}
					else = {
						add_character_modifier = {
							modifier = crusader_momentum_modifier_large
							years = 10
						}
					}
				}
				else = {
					if = {
						limit = {
							is_ai = yes
						}
						add_character_modifier = {
							modifier = crusader_momentum_modifier
							years = 10
							desc = crusader_momentum_ai_behavior_tt
						}
						custom_tooltip = crusader_momentum_ai_behavior_tt
					}
					else = {
						add_character_modifier = {
							modifier = crusader_momentum_modifier
							years = 10
						}
					}
				}
			}
			if = {
				limit = {
					exists = scope:target
				}
				add_prestige = {
					value = minor_prestige_loss
					multiply = {
						value = scope:target.tier
						subtract = 1
					}
				}
			}
			else = {
				add_prestige = medium_prestige_loss
			}
		}
		if = {
			limit = {
				scope:inspire_realm ?= yes
			}
			scope:secondary_recipient = {
				add_character_modifier = {
					modifier = holy_war_incited_throughout_realm_modifier
					years = 15
				}
			}
		}
		#Discount to expanding archbishopric into the area
		if = {
			limit = {
				pam_can_expand_clerical_region_after_war_trigger = { HEATHEN = scope:recipient }
			}
			custom_tooltip = can_expand_clerical_region_into_target_tt
		}
	}
	scope:recipient = {
		trigger_event = {
			id = pam_interactions_events.0072
			days = 3
		}
	}
	scope:puppet_or_actor = {
		if = {
			limit = {
				exists = scope:target
				scope:target.tier >= tier_kingdom
				any_character_situation = {
					situation_type = the_christian_church
				}
				situation:the_christian_church ?= {
					situation_top_has_catalyst = catalyst_major_holy_war_incited
				}
			}
			trigger_situation_catalyst = catalyst_major_holy_war_incited
		}
		else_if = {
			limit = {
				exists = scope:target
				scope:target.tier >= tier_duchy
				any_character_situation = {
					situation_type = the_christian_church
				}
				situation:the_christian_church ?= {
					situation_top_has_catalyst = catalyst_holy_war_incited
				}
			}
			trigger_situation_catalyst = catalyst_holy_war_incited
		}
		else = {
			limit = {
				any_character_situation = {
					situation_type = the_christian_church
				}
				situation:the_christian_church ?= {
					situation_top_has_catalyst = catalyst_minor_holy_war_incited
				}
			}
			trigger_situation_catalyst = catalyst_minor_holy_war_incited
		}
	}
}
pam_incite_war_refusal_effect = {
	if = {
		limit = {
			scope:target.tier >= tier_kingdom
		}
		add_piety_level = -1 
	}
	else = {
		add_piety_experience = {
			value = major_piety_loss
			multiply = {
				value = scope:target.tier
				subtract = 1
			}
		}
	}
	
}
pam_redirect_request_excommunication_ruler_effect = {
	#Is the secondary recipient a theocrat? Only their superior can excommunicate them
	if = {
		limit = {
			scope:secondary_recipient = {
				government_has_flag = government_is_theocracy
				exists = superior
				NOR = {
					superior = scope:secondary_recipient
					superior = scope:actor
				}
			}
		}
		scope:secondary_recipient.superior = {
			save_scope_as = recipient
		}
	}
	#Redirect to bishop superior of court chaplain
	else_if = {
		limit = {
			scope:secondary_recipient.cp:councillor_court_chaplain ?= {
				highest_held_title_tier < tier_duchy
				superior ?= {
					highest_held_title_tier >= tier_duchy
					is_clergy = yes
					NOT = {
						this = scope:actor
					}
				}
			}
		}
		scope:secondary_recipient.cp:councillor_court_chaplain.superior = {
			save_scope_as = recipient
		}
	}
	#Redirect to ecclesiastical/theocratic liege or above
	else_if = {
		limit = {
			scope:secondary_recipient = {
				any_liege_or_above = {
					highest_held_title_tier >= tier_duchy
					government_has_flag = government_is_theocracy
					faith ?= scope:secondary_recipient.faith
					NOT = {
						this = scope:actor
					}
				}
			}
		}
		scope:secondary_recipient = {
			ordered_liege_or_above = {
				order_by = highest_held_title_tier
				limit = {
					highest_held_title_tier >= tier_duchy
					government_has_flag = government_is_theocracy
					faith ?= scope:secondary_recipient.faith
					NOT = {
						this = scope:actor
					}
				}
				save_scope_as = recipient
			}
		}
	}
	#Redirect to court chaplain of liege or above
	else_if = {
		limit = {
			scope:secondary_recipient = {
				any_liege_or_above = {
					cp:councillor_court_chaplain ?= {
						highest_held_title_tier < tier_duchy
						is_clergy = yes
						NOT = {
							this = scope:actor
						}
					}
				}
			}
		}
		scope:secondary_recipient = {
			ordered_liege_or_above = {
				order_by = {
					value = highest_held_title_tier
					multiply = -1
				}
				limit = {
					cp:councillor_court_chaplain ?= {
						highest_held_title_tier < tier_duchy
						is_clergy = yes
						NOT = {
							this = scope:actor
						}
					}
				}
				cp:councillor_court_chaplain = {
					save_scope_as = recipient
				}
			}
		}
	}
	#Redirect to bishop
	else_if = {
		limit = {
			scope:secondary_recipient.cp:councillor_court_chaplain ?= {
				highest_held_title_tier >= tier_duchy
				is_clergy = yes
				NOT = {
					this = scope:actor
				}
			}
		}
		scope:secondary_recipient.cp:councillor_court_chaplain = {
			save_scope_as = recipient
		}
	}
	#Redirect to clerical region_holder
	else_if = {
		limit = {
			scope:secondary_recipient.capital_county = {
				clerical_region_title ?= {
					holder ?= {
						highest_held_title_tier >= tier_duchy
						faith ?= scope:secondary_recipient.faith
						NOT = {
							this = scope:actor
						}
					}
				}
			}
		}
		scope:secondary_recipient.capital_county.clerical_region_title.holder = {
			save_scope_as = recipient
		}
	}
	#Redirect to neighboring clerical region_holder
	else_if = {
		limit = {
			scope:secondary_recipient = {
				any_character_to_title_neighboring_county = {
					clerical_region_title ?= {
						holder ?= {
							highest_held_title_tier >= tier_duchy
							faith ?= scope:secondary_recipient.faith
							NOT = {
								this = scope:actor
							}
						}
					}
				}
			}
		}
		scope:secondary_recipient = {
			random_character_to_title_neighboring_county = {
				limit = {
					clerical_region_title ?= {
						holder ?= {
							highest_held_title_tier >= tier_duchy
							faith ?= scope:secondary_recipient.faith
							NOT = {
								this = scope:actor
							}
						}
					}
				}
				clerical_region_title.holder = {
					save_scope_as = recipient
				}
			}
		}
	}
}
pam_redirect_request_excommunication_courtier_effect = {
	#liege
	if = {
		limit = {
			scope:secondary_recipient = {
				liege = {
					highest_held_title_tier >= tier_duchy
					government_has_flag = government_is_theocracy
					faith ?= scope:secondary_recipient.faith
					NOT = {
						this = scope:actor
					}
				}
			}
		}
		scope:secondary_recipient.liege = {
			save_scope_as = recipient
		}
	}
	#Redirect to court chaplain of liege or above
	else_if = {
		limit = {
			scope:secondary_recipient = {
				any_liege_or_above = {
					cp:councillor_court_chaplain ?= {
						highest_held_title_tier < tier_duchy
						is_clergy = yes
						NOT = {
							this = scope:actor
						}
					}
				}
			}
		}
		scope:secondary_recipient = {
			ordered_liege_or_above = {
				order_by = {
					value = highest_held_title_tier
					multiply = -1
				}
				limit = {
					cp:councillor_court_chaplain ?= {
						highest_held_title_tier < tier_duchy
						is_clergy = yes
						NOT = {
							this = scope:actor
						}
					}
				}
				cp:councillor_court_chaplain = {
					save_scope_as = recipient
				}
			}
		}
	}
	#Redirect to clerical region_holder
	else_if = {
		limit = {
			scope:secondary_recipient.liege.capital_county = {
				clerical_region_title ?= {
					holder ?= {
						highest_held_title_tier >= tier_duchy
						faith ?= scope:secondary_recipient.faith
						NOT = {
							this = scope:actor
						}
					}
				}
			}
		}
		scope:secondary_recipient.liege.capital_county.clerical_region_title.holder = {
			save_scope_as = recipient
		}
	}
	#Redirect to ecclesiastical/theocratic liege or above
	else_if = {
		limit = {
			scope:secondary_recipient = {
				any_liege_or_above = {
					highest_held_title_tier >= tier_duchy
					government_has_flag = government_is_theocracy
					faith ?= scope:secondary_recipient.faith
					NOT = {
						this = scope:actor
					}
				}
			}
		}
		scope:secondary_recipient = {
			ordered_liege_or_above = {
				order_by = {
					value = highest_held_title_tier
					multiply = -1
				}
				limit = {
					highest_held_title_tier >= tier_duchy
					government_has_flag = government_is_theocracy
					faith ?= scope:secondary_recipient.faith
					NOT = {
						this = scope:actor
					}
				}
				save_scope_as = recipient
			}
		}
	}
	#Redirect to bishop superior of court chaplain
	else_if = {
		limit = {
			scope:secondary_recipient.liege.cp:councillor_court_chaplain ?= {
				highest_held_title_tier < tier_duchy
				superior ?= {
					highest_held_title_tier >= tier_duchy
					is_clergy = yes
					NOT = {
						this = scope:actor
					}
				}
			}
		}
		scope:secondary_recipient.liege.cp:councillor_court_chaplain.superior = {
			save_scope_as = recipient
		}
	}
	#Redirect to neighboring clerical region_holder
	else_if = {
		limit = {
			scope:secondary_recipient.liege = {
				any_character_to_title_neighboring_county = {
					clerical_region_title ?= {
						holder ?= {
							highest_held_title_tier >= tier_duchy
							faith ?= scope:secondary_recipient.faith
							NOT = {
								this = scope:actor
							}
						}
					}
				}
			}
		}
		scope:secondary_recipient.liege = {
			random_character_to_title_neighboring_county = {
				limit = {
					clerical_region_title ?= {
						holder ?= {
							highest_held_title_tier >= tier_duchy
							faith ?= scope:secondary_recipient.faith
							NOT = {
								this = scope:actor
							}
						}
					}
				}
				clerical_region_title.holder = {
					save_scope_as = recipient
				}
			}
		}
	}
}
pam_redirect_request_excommunication_wanderer_effect = {
	#Location holder
	if = {
		limit = {
			scope:secondary_recipient.location.county.holder ?= {
				highest_held_title_tier >= tier_duchy
				government_has_flag = government_is_theocracy
				faith ?= scope:secondary_recipient.faith
				NOT = {
					this = scope:actor
				}
			}
		}
		scope:secondary_recipient.location.county.holder ?= {
			save_scope_as = recipient
		}
	}
	#Location holder
	if = {
		limit = {
			scope:secondary_recipient.location.county.holder.cp:councillor_court_chaplain ?= {
				highest_held_title_tier >= tier_duchy
				is_clergy = yes
				faith ?= scope:secondary_recipient.faith
				NOT = {
					this = scope:actor
				}
			}
		}
		scope:secondary_recipient.location.county.holder.cp:councillor_court_chaplain ?= {
			save_scope_as = recipient
		}
	}
	#Redirect to court chaplain of holder's liege or above
	else_if = {
		limit = {
			scope:secondary_recipient.location.county.holder ?= {
				any_liege_or_above = {
					cp:councillor_court_chaplain ?= {
						highest_held_title_tier < tier_duchy
						is_clergy = yes
						NOT = {
							this = scope:actor
						}
					}
				}
			}
		}
		scope:secondary_recipient.location.county.holder ?= {
			ordered_liege_or_above = {
				order_by = {
					value = highest_held_title_tier
					multiply = -1
				}
				limit = {
					cp:councillor_court_chaplain ?= {
						highest_held_title_tier < tier_duchy
						is_clergy = yes
						NOT = {
							this = scope:actor
						}
					}
				}
				cp:councillor_court_chaplain = {
					save_scope_as = recipient
				}
			}
		}
	}
	#Redirect to ecclesiastical/theocratic liege or above
	else_if = {
		limit = {
			scope:secondary_recipient.location.county.holder ?= {
				any_liege_or_above = {
					highest_held_title_tier >= tier_duchy
					government_has_flag = government_is_theocracy
					faith ?= scope:secondary_recipient.faith
					NOT = {
						this = scope:actor
					}
				}
			}
		}
		scope:secondary_recipient.location.county.holder ?= {
			ordered_liege_or_above = {
				order_by = {
					value = highest_held_title_tier
					multiply = -1
				}
				limit = {
					highest_held_title_tier >= tier_duchy
					government_has_flag = government_is_theocracy
					faith ?= scope:secondary_recipient.faith
					NOT = {
						this = scope:actor
					}
				}
				save_scope_as = recipient
			}
		}
	}
	#Redirect to clerical region_holder
	else_if = {
		limit = {
			scope:secondary_recipient.location.county ?= {
				clerical_region_title ?= {
					holder ?= {
						highest_held_title_tier >= tier_duchy
						faith ?= scope:secondary_recipient.faith
						NOT = {
							this = scope:actor
						}
					}
				}
			}
		}
		scope:secondary_recipient.location.county.clerical_region_title.holder = {
			save_scope_as = recipient
		}
	}
	#Redirect to neighboring clerical region_holder
	else_if = {
		limit = {
			scope:secondary_recipient.location.county ?= {
				any_neighboring_county = {
					clerical_region_title ?= {
						holder ?= {
							highest_held_title_tier >= tier_duchy
							faith ?= scope:secondary_recipient.faith
							NOT = {
								this = scope:actor
							}
						}
					}
				}
			}
		}
		scope:secondary_recipient.location.county = {
			random_neighboring_county = {
				limit = {
					clerical_region_title ?= {
						holder ?= {
							highest_held_title_tier >= tier_duchy
							faith ?= scope:secondary_recipient.faith
							NOT = {
								this = scope:actor
							}
						}
					}
				}
				clerical_region_title.holder = {
					save_scope_as = recipient
				}
			}
		}
	}
}
pam_christian_situation_hof_incite_holy_war_effect = {
	if = {
		limit = {
			faith.religious_head ?= root
			is_ai = yes
			top_participant_group:the_christian_church ?= {
				has_participant_group_parameter = the_christian_church_incite_holy_war_interaction
			}
			piety >= 100
			piety_level >= 2
			is_available_quick = {
				adult = yes
				alive = yes
				imprisoned = no
				incapable = no
			}
			actor_incite_holy_war_shown_trigger = yes
		}
		save_scope_as = puppet_or_actor
		situation:the_christian_church = {
			situation_participant_group:christian_regular_main_power_rulers = {
				random_situation_group_participant = {
					limit = {
						faith = scope:puppet_or_actor.faith
						highest_held_title_tier >= tier_county
						is_at_war = no
						save_temporary_scope_as = secondary_temp
						#First need to find a neighbor who works...
						any_neighboring_top_liege_realm_owner = {
							save_temporary_scope_as = recipient_temp
							NOT = {
								faith = scope:secondary_temp.faith
							}
							scope:secondary_temp.faith = {
								faith_hostility_level = {
									target = scope:recipient_temp.faith
									value >= religious_cb_enabled_hostility_level
								}
							}
							NOR = {
								is_at_war_with = scope:puppet_or_actor
								is_at_war_with = scope:secondary_temp
							}
							current_military_strength < scope:secondary_temp.eighty_percent_of_current_military_strength
							scope:secondary_temp = {
								valid_holy_war_incited_attacker_trigger = { RECIPIENT = scope:recipient_temp }
							}
							incite_holy_war_ai_invalidation_trigger = { RECIPIENT = scope:recipient_temp SECONDARY = scope:secondary_temp }
							trigger_if = {
								limit = {
									scope:puppet_or_actor.piety_level >= 3
									highest_held_title_tier >= tier_duchy
									realm_size >= 8
								}
								any_realm_de_jure_kingdom = {
									incite_holy_war_title_can_be_picked_trigger = { RECIPIENT = scope:recipient_temp SECONDARY = scope:secondary_temp }
								}
							}
							trigger_else_if = {
								limit = {
									scope:puppet_or_actor.piety_level >= 2
									realm_size >= 2
								}
								any_realm_de_jure_duchy = {
									incite_holy_war_title_can_be_picked_trigger = { RECIPIENT = scope:recipient_temp SECONDARY = scope:secondary_temp }
								}
							}
							trigger_else = {
								any_realm_county = {
									incite_holy_war_title_can_be_picked_trigger = { RECIPIENT = scope:recipient_temp SECONDARY = scope:secondary_temp }
								}
							}
						}
					}
					save_scope_as = secondary_recipient
					ordered_neighboring_top_liege_realm_owner = {
						order_by = {
							value = current_military_strength 
							multiply = -1
						}
						limit = {
							save_temporary_scope_as = recipient_temp
							NOT = {
								faith = scope:secondary_recipient.faith
							}
							scope:secondary_recipient.faith = {
								faith_hostility_level = {
									target = scope:recipient_temp.faith
									value >= religious_cb_enabled_hostility_level
								}
							}
							NOR = {
								is_at_war_with = scope:puppet_or_actor
								is_at_war_with = scope:secondary_recipient
							}
							current_military_strength < scope:secondary_recipient.eighty_percent_of_current_military_strength
							scope:secondary_recipient = {
								valid_holy_war_incited_attacker_trigger = { RECIPIENT = scope:recipient_temp }
							}
							incite_holy_war_ai_invalidation_trigger = { RECIPIENT = scope:recipient_temp SECONDARY = scope:secondary_recipient }
							trigger_if = {
								limit = {
									scope:puppet_or_actor.piety_level >= 3
									highest_held_title_tier >= tier_duchy
									realm_size >= 8
								}
								any_realm_de_jure_kingdom = {
									incite_holy_war_title_can_be_picked_trigger = { RECIPIENT = scope:recipient_temp SECONDARY = scope:secondary_recipient }
								}
							}
							trigger_else_if = {
								limit = {
									scope:puppet_or_actor.piety_level >= 2
									realm_size >= 2
								}
								any_realm_de_jure_duchy = {
									incite_holy_war_title_can_be_picked_trigger = { RECIPIENT = scope:recipient_temp SECONDARY = scope:secondary_recipient }
								}
							}
							trigger_else = {
								any_realm_county = {
									incite_holy_war_title_can_be_picked_trigger = { RECIPIENT = scope:recipient_temp SECONDARY = scope:secondary_recipient }
								}
							}
						}
						save_scope_as = recipient
						#Save target
						if = {
							limit = {
								scope:puppet_or_actor.piety_level >= 3
								highest_held_title_tier >= tier_duchy
								realm_size >= 8
							}
							ordered_realm_de_jure_kingdom = {
								order_by = de_jure_counties_held_by_recipient_value
								limit = {
									incite_holy_war_title_can_be_picked_trigger = { RECIPIENT = scope:recipient SECONDARY = scope:secondary_recipient }
								}
								save_scope_as = target
							}
						}
						else_if = {
							limit = {
								scope:puppet_or_actor.piety_level >= 2
								realm_size >= 2
							}
							ordered_realm_de_jure_duchy = {
								order_by = de_jure_counties_held_by_recipient_value
								limit = {
									incite_holy_war_title_can_be_picked_trigger = { RECIPIENT = scope:recipient SECONDARY = scope:secondary_recipient }
								}
								save_scope_as = target
							}
						}
						else = {
							random_realm_county = {
								limit = {
									incite_holy_war_title_can_be_picked_trigger = { RECIPIENT = scope:recipient SECONDARY = scope:secondary_recipient }
								}
								save_scope_as = target
							}
						}
					}
				}
			}
		}
		save_scope_as = actor
		if = {
			limit = {
				exists = scope:secondary_recipient
				exists = scope:recipient
			}
			ecclesiastic_incite_holy_war_interaction_effect = yes
		}
	}
}
ecclesiastic_incite_holy_war_interaction_effect = {
	scope:target = {
		save_scope_as = sponsored_holy_war_target
	}
	show_as_tooltip = {
		pam_incite_holy_war_effect = yes
		scope:recipient = { custom_tooltip = holy_war_joining_interaction_warning_tt }
	}
	#Secondary recipient gets to call this off if player
	if = {
		limit = {
			scope:secondary_recipient = {
				is_ai = no
			}
		}
		scope:secondary_recipient = {
			trigger_event = pam_interactions_events.0070
		}
	}
	else = {
		scope:actor = {
			trigger_event = pam_interactions_events.0071
		}
	}
}

pam_notify_claim_war_instigator_effect_win = {
	if = {
		limit = {
			scope:war ?= {
				has_variable = claim_instigator
				var:claim_instigator = {
					is_alive = yes
					save_temporary_scope_as = instigator_temp
				}
				OR = {
					has_variable = claim_joiner
					NOT = {
						any_war_participant = {
							this = scope:instigator_temp
						}
					}
				}
			}
		}
		every_in_list = {
			list = target_titles
			save_scope_as = target
		}
		scope:war = {
			var:claim_instigator = {
				send_interface_message = {
					type = event_generic_good
					title = pam_instigated_claim_war_won.t
					left_icon = scope:attacker
					right_icon = scope:defender
					if = {
						limit = {
							is_ai = no
						}
						add_prestige = pam_holy_war_inciter_prestige_value
					}
					else = {
						hidden_effect = {
							add_prestige = pam_holy_war_inciter_prestige_value
						}
					}
					custom_tooltip = pam_instigated_claim_war_won_tt
				}
			}
		}
	}
}
pam_notify_claim_war_instigator_effect_draw = {
	if = {
		limit = {
			scope:war ?= {
				has_variable = claim_instigator
				var:claim_instigator = {
					is_alive = yes
					save_temporary_scope_as = instigator_temp
				}
				OR = {
					has_variable = claim_joiner
					NOT = {
						any_war_participant = {
							this = scope:instigator_temp
						}
					}
				}
			}
		}
		every_in_list = {
			list = target_titles
			save_scope_as = target
		}
		scope:war = {
			var:claim_instigator = {
				send_interface_message = {
					type = event_generic_bad
					title = pam_instigated_claim_war_draw.t
					left_icon = scope:attacker
					right_icon = scope:defender
					custom_tooltip = pam_instigated_claim_war_draw_tt
				}
			}
		}
	}
}
pam_notify_claim_war_instigator_effect_loss = {
	if = {
		limit = {
			scope:war ?= {
				has_variable = claim_instigator
				var:claim_instigator = {
					is_alive = yes
					save_temporary_scope_as = instigator_temp
				}
				OR = {
					has_variable = claim_joiner
					NOT = {
						any_war_participant = {
							this = scope:instigator_temp
						}
					}
				}
			}
		}
		every_in_list = {
			list = target_titles
			save_scope_as = target
		}
		scope:war = {
			if = {
				limit = {
					has_variable = claim_joiner
				}
				var:claim_joiner = {
					send_interface_message = {
						type = event_generic_bad
						title = pam_instigated_claim_war_lost.t
						left_icon = scope:attacker
						right_icon = scope:defender
						add_prestige_level = -2
					}
				}
			}
			else = {
				var:claim_instigator = {
					send_interface_message = {
						type = event_generic_bad
						title = pam_instigated_claim_war_lost.t
						left_icon = scope:attacker
						right_icon = scope:defender
						custom_tooltip = pam_instigated_claim_war_lost_tt
					}
				}
			}
		}
	}
}
pam_incite_claim_war_effect = {
	scope:secondary_recipient = {
		save_scope_as = sponsored_claim_war_attacker
		add_piety = {
			value = medium_piety_value
			multiply = {
				value = scope:target.tier
				subtract = 1
			}
		}
	}
	scope:recipient = {
		save_scope_as = sponsored_claim_war_defender
		if = {
			limit = {
				NOT = {
					root ?= scope:secondary_recipient
				}
			}
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = incited_claim_war_opinion
				opinion = -50
			}
		}
	}
	if = {
		limit = {
			root ?= scope:secondary_recipient
		}
		custom_tooltip = interaction_claim_war_attacker_tt
		show_as_tooltip = {
			scope:recipient = {
				add_opinion = {
					modifier = declared_war
					target = scope:secondary_recipient
				}
			}
		}
	}
	else = {
		scope:recipient = {
			custom_tooltip = interaction_claim_war_attacked_tt
		}
	}

	scope:sponsored_claim_war_attacker = {
		add_character_flag = {
			flag = temp_no_claim_war_cost
			days = 10
		}
		hidden_effect = {
			start_war = {
				casus_belli = claim_cb
				target = scope:sponsored_claim_war_defender
				claimant = scope:sponsored_claim_war_attacker
				target_title = scope:sponsored_claim_war_target
			}
			remove_character_flag = temp_no_claim_war_cost
		}
		random_character_war = {
			limit = {
				using_cb = claim_cb
				primary_defender = scope:recipient
			}
			save_scope_as = claim_war
			set_variable = {
				name = claim_instigator
				value = scope:puppet_or_actor
			}
		}
	}
	scope:puppet_or_actor = {
		custom_tooltip = inciter_gain_prestige_if_claim_war_won_tt
		if = {
			limit = {
				scope:fund_war ?= yes
			}
			pay_treasury_or_gold = {
				target = scope:secondary_recipient
				value = scope:puppet_or_actor.major_gold_value
			}
		}
		if = {
			limit = {
				scope:join_war ?= yes
			}
			custom_tooltip = claim_war_interaction_join_war_tt
			custom_tooltip = holy_war_cleric_loss_penalty_tt
			scope:claim_war ?= {
				hidden_effect = { add_attacker = scope:puppet_or_actor }
				set_variable = {
					name = claim_joiner
					value = scope:puppet_or_actor
				}
			}
		}
		if = {
			limit = {
				scope:use_hook ?= yes
				has_usable_hook = scope:secondary_recipient
			}
			use_hook = scope:secondary_recipient
		}
	}
}
ecclesiastic_incite_claim_war_interaction_effect = {
	scope:target = {
		save_scope_as = sponsored_claim_war_target
	}
	show_as_tooltip = {
		pam_incite_claim_war_effect = yes
	}
	hidden_effect = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = ecclesiastic_incite_claim_war_interaction_toast
				desc = incite_claim_war_past_tense_tt
				left_icon = scope:secondary_recipient
				right_icon = scope:recipient
				pam_incite_claim_war_effect = yes
			}
		}
		if = {
			limit = {
				scope:secondary_recipient = {
					is_ai = no
					this != scope:actor
				}
			}
			scope:secondary_recipient = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = ecclesiastic_incite_claim_war_interaction_attacker_toast
					desc = incite_claim_war_past_tense_tt
					left_icon = scope:puppet_or_actor
					right_icon = scope:recipient
					if = {
						limit = { scope:join_war ?= yes }
						custom_tooltip = incite_claim_war_joined_tt
					}
					else_if = {
						limit = { scope:fund_war ?= yes }
						custom_tooltip = incite_claim_war_funded_tt
					}
				}
			}
		}
		if = {
			limit = {
				scope:recipient = {
					is_ai = no
					this != scope:actor
					this != scope:secondary_recipient
				}
			}
			scope:recipient = {
				send_interface_toast = {
					type = event_toast_effect_bad
					title = ecclesiastic_incite_claim_war_interaction_defender_toast
					desc = incite_claim_war_past_tense_tt
					left_icon = scope:secondary_recipient
					right_icon = scope:puppet_or_actor
					if = {
						limit = { scope:join_war ?= yes }
						custom_tooltip = incite_claim_war_joined_tt
					}
				}
			}
		}
	}
}

request_white_peace_interaction_effect = {
	scope:puppet_or_actor = {
		random_character_war = {
			limit = {
				pam_request_white_peace_war_with_enemy_trigger = { ENEMY = scope:secondary_recipient }
			}
			save_scope_as = white_peace_war
			save_scope_as = war #release_prisoners_of_war_effect resolves the war through scope:war
		}
	}
	scope:white_peace_war = {
		casus_belli = {
			primary_attacker = {
				save_scope_as = attacker
			}
			primary_defender = {
				save_scope_as = defender
			}
		}
		release_prisoners_of_war_effect = yes
		end_war = white_peace
	}
	scope:recipient = {
		send_interface_toast = {
			type = event_toast_effect_good
			title = request_white_peace_interaction_recipient_toast
			left_icon = scope:puppet_or_actor
			right_icon = scope:secondary_recipient
			if = {
				limit = {
					OR = {
						rite = {
							OR = {
								has_tenet_status = { tenet = tenet_peace_of_god status = core }
								has_tenet_status = { tenet = tenet_pacifism status = core }
								has_tenet_status = { tenet = tenet_dharmic_pacifism status = core }
							}
						}
						any_personal_tenet = {
							OR = {
								this = tenet:tenet_peace_of_god
								this = tenet:tenet_pacifism
								this = tenet:tenet_dharmic_pacifism
							}
						}
					}
				}
				change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			}
			else_if = {
				limit = {
					NOT = { has_character_modifier = pam_white_peace_enforcer_modifier }
				}
				add_character_modifier = {
					modifier = pam_white_peace_enforcer_modifier
					years = 10
				}
			}
			if = {
				limit = { has_any_nickname = no }
				hidden_effect = {
					random = {
						chance = {
							value = 1
							if = {
								limit = { is_ai = no }
								add = 9
							}
						}
						give_nickname = nick_the_peacemaker
					}
				}
			}
		}
	}
	scope:secondary_recipient = {
		if = {
			limit = { is_ai = yes }
			add_opinion = {
				target = scope:recipient
				modifier = pam_enforced_white_peace_opinion
			}
		}
		trigger_event = pam_interactions_events.0080
	}
	if = {
		limit = {
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_the_christian_church_white_peace_enforced
			}
		}
		situation:the_christian_church = {
			trigger_situation_catalyst = catalyst_the_christian_church_white_peace_enforced
		}
	}
	scope:puppet_or_actor = {
		if = {
			limit = {
				scope:donate_faith = yes
			}
			pay_treasury_or_gold = {
				target = scope:recipient
				value = major_gold_value
			}
		}
		if = {
			limit = {
				scope:offer_piety = yes
			}
			add_piety = major_piety_loss
			scope:recipient = {
				add_piety = major_piety_gain
			}
		}
		if = {
			limit = {
				scope:offer_hook = yes
			}
			scope:recipient = {
				add_hook = {
					type = favor_hook
					target = scope:puppet_or_actor
				}
			}
		}
		pam_offer_pilgrimage_effect = {
			ACTOR = scope:puppet_or_actor
			CLERIC = scope:recipient
		}
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = request_white_peace_interaction_toast_title
			custom_tooltip = request_white_peace_interaction_toast
			left_icon = scope:recipient
			right_icon = scope:secondary_recipient
		}
	}
	if = {
		limit = {
			scope:is_puppet_action ?= yes
			scope:actor != scope:recipient
		}
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = request_white_peace_interaction_toast_title
				custom_tooltip = request_white_peace_interaction_toast_puppeteer
				left_icon = scope:puppet_or_actor
				right_icon = scope:recipient
			}
		}
	}
}

request_victory_interaction_effect = {
	scope:puppet_or_actor = {
		random_character_war = {
			limit = {
				pam_request_victory_war_with_enemy_trigger = { ENEMY = scope:secondary_recipient }
			}
			save_scope_as = requested_victory_war
			save_scope_as = war #release_prisoners_of_war_effect resolves the war through scope:war
		}
	}
	if = {
		#The war can end during the reply window, leaving nothing to rule on
		limit = { exists = scope:requested_victory_war }
		scope:requested_victory_war = {
			casus_belli = {
				primary_attacker = {
					save_scope_as = attacker
				}
				primary_defender = {
					save_scope_as = defender
				}
			}
			release_prisoners_of_war_effect = yes
			if = {
				limit = {
					casus_belli = {
						primary_attacker = scope:puppet_or_actor
					}
				}
				end_war = attacker
			}
			else = {
				end_war = defender
			}
		}
		scope:recipient = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = request_victory_interaction_recipient_toast
				left_icon = scope:puppet_or_actor
				right_icon = scope:secondary_recipient
			}
		}
		#The Church makes it worth the loser's while, so that obeying a ruling is not simply a defeat
		scope:secondary_recipient = {
			if = {
				limit = {
					NOT = { has_character_modifier = pam_church_compensation_modifier }
				}
				add_character_modifier = {
					modifier = pam_church_compensation_modifier
					years = 10
				}
			}
			if = {
				limit = { is_ai = yes }
				add_opinion = {
					target = scope:recipient
					modifier = pam_enforced_victory_opinion
				}
			}
			trigger_event = pam_interactions_events.0083
		}
		if = {
			limit = {
				situation:the_christian_church ?= {
					situation_top_has_catalyst = catalyst_the_christian_church_victory_enforced
				}
			}
			situation:the_christian_church = {
				trigger_situation_catalyst = catalyst_the_christian_church_victory_enforced
			}
		}
		scope:puppet_or_actor = {
			if = {
				limit = {
					scope:donate_faith = yes
				}
				pay_treasury_or_gold = {
					target = scope:recipient
					value = major_gold_value
				}
			}
			if = {
				limit = {
					scope:offer_piety = yes
				}
				add_piety = major_piety_loss
				scope:recipient = {
					add_piety = major_piety_gain
				}
			}
			if = {
				limit = {
					scope:offer_hook = yes
				}
				scope:recipient = {
					add_hook = {
						type = favor_hook
						target = scope:puppet_or_actor
					}
				}
			}
			pam_offer_pilgrimage_effect = {
				ACTOR = scope:puppet_or_actor
				CLERIC = scope:recipient
			}
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = request_victory_interaction_toast_title
				custom_tooltip = request_victory_interaction_toast
				left_icon = scope:recipient
				right_icon = scope:secondary_recipient
			}
		}
		if = {
			limit = {
				scope:is_puppet_action ?= yes
				scope:actor != scope:recipient
			}
			scope:actor = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = request_victory_interaction_toast_title
					custom_tooltip = request_victory_interaction_toast_puppeteer
					left_icon = scope:puppet_or_actor
					right_icon = scope:recipient
				}
			}
		}
	}
}

pam_enforce_title_succession_effect = {
	scope:target = { save_scope_as = succession_title }
	scope:target.holder = { save_scope_as = succession_holder }
	scope:succession_title = {
		current_heir = { save_scope_as = enforced_heir }
	}
	if = {
		limit = {
			exists = scope:enforced_heir
			NOT = { scope:enforced_heir = scope:succession_holder }
		}
		create_title_and_vassal_change = {
			type = stepped_down
			save_scope_as = succession_title_change
			add_claim_on_loss = no
		}
		scope:succession_title = {
			change_title_holder_include_vassals = {
				holder = scope:enforced_heir
				change = scope:succession_title_change
			}
		}
		resolve_title_and_vassal_change = scope:succession_title_change
	}
	if = {
		limit = { scope:succession_holder = { is_ai = yes } }
		scope:succession_holder = {
			add_opinion = {
				target = scope:actor
				modifier = meddling_church_opinion
			}
		}
	}
}

pam_increase_papabile_score_effect = {
	if = {
		limit = { scope:pam_papabile_gold_option = yes }
		custom_tooltip = {
			text = pam_increase_papabile_score_effect_20_tt
			if = {
				limit = { exists = var:pam_papabile_score }
				change_variable = {
					name = pam_papabile_score
					add = $VALUE$
				}
			}
			else = {
				set_variable = {
					name = pam_papabile_score
					value = $VALUE$
				}
			}
		}
		if = {
			limit = { this != scope:actor }
			add_opinion = {
				target = scope:actor
				modifier = grateful_opinion
				opinion = 30
			}
		}
	}
	else = {
		custom_tooltip = {
			text = pam_increase_papabile_score_effect_10_tt
			if = {
				limit = { exists = var:pam_papabile_score }
				change_variable = {
					name = pam_papabile_score
					add = $VALUE$
				}
			}
			else = {
				set_variable = {
					name = pam_papabile_score
					value = $VALUE$
				}
			}
		}
		if = {
			limit = { this != scope:actor }
			add_opinion = {
				target = scope:actor
				modifier = grateful_opinion
				opinion = 15
			}
		}
	}
	# Both branches above have already created the variable
	if = {
		limit = {
			scope:actor ?= {
				has_personal_tenet_flag = tenet_apostolic_succession_papabile_influence
			}
		}
		custom_tooltip = {
			text = pam_increase_papabile_score_apostolic_succession_tt
			change_variable = {
				name = pam_papabile_score
				add = pam_apostolic_succession_papabile_bonus_value
			}
		}
	}
	if = {
		limit = { exists = scope:actor }
		custom_tooltip = {
			text = pam_papabile_leaning_tt
			pam_spread_papabile_leaning_effect = { VALUE = $VALUE$ }
		}
	}
}

pam_spread_papabile_leaning_effect = {
	save_scope_as = pam_papabile_candidate
	faith = {
		every_clerical_elector_title = {
			limit = {
				holder ?= {
					this != scope:pam_papabile_candidate
					OR = {
						this = scope:actor
						is_vassal_or_below_of = scope:actor
						top_liege ?= scope:actor.top_liege
						is_close_or_extended_family_of = scope:actor
						scope:actor.puppet:theological_agent_puppet ?= this
						scope:actor.puppet:external_theological_agent_puppet ?= this
						opinion = {
							target = scope:actor
							value >= medium_positive_opinion
						}
					}
				}
			}
			holder ?= {
				pam_apply_papabile_leaning_effect = { VALUE = $VALUE$ }
			}
		}
	}
}

pam_apply_papabile_leaning_effect = {
	set_variable = {
		name = pam_papabile_leaning_target
		value = scope:pam_papabile_candidate
		years = 20
	}
	set_variable = {
		name = pam_papabile_leaning_value
		value = $VALUE$
		years = 20
	}
}

# We declare the cardinal for that seat, otherwise a holy candidate is listed as The Pious
pam_place_first_as_powerful_effect = {
	custom_tooltip = {
		text = pam_place_first_as_powerful_tt
		if = {
			limit = { exists = var:pam_papabile_score }
			change_variable = {
				name = pam_papabile_score
				add = $VALUE$
			}
		}
		else = {
			set_variable = {
				name = pam_papabile_score
				value = $VALUE$
			}
		}
	}
	custom_tooltip = {
		text = pam_campaign_as_powerful_tt
		remove_variable = pam_campaign_as_populist
		set_variable = {
			name = pam_campaign_as_powerful
			years = 20
		}
	}
	# Deliberately the flat nudge rather than the purchased score: buying the seat should not also buy the vote.
	if = {
		limit = { exists = scope:actor }
		custom_tooltip = {
			text = pam_papabile_leaning_tt
			pam_spread_papabile_leaning_effect = { VALUE = 10 }
		}
	}
}

pam_place_first_as_populist_effect = {
	# Own variable, and the elector vote modifier also reads pam_propagate_word_of_my_piety the same way
	custom_tooltip = {
		text = pam_place_first_as_populist_tt
		if = {
			limit = { exists = var:pam_papabile_populist_score }
			change_variable = {
				name = pam_papabile_populist_score
				add = $VALUE$
			}
		}
		else = {
			set_variable = {
				name = pam_papabile_populist_score
				value = $VALUE$
			}
		}
	}
	custom_tooltip = {
		text = pam_campaign_as_populist_tt
		remove_variable = pam_campaign_as_powerful
		set_variable = {
			name = pam_campaign_as_populist
			years = 20
		}
	}
	# Deliberately the flat nudge rather than the purchased score, matching the Powerful path.
	if = {
		limit = { exists = scope:actor }
		custom_tooltip = {
			text = pam_papabile_leaning_tt
			pam_spread_papabile_leaning_effect = { VALUE = 10 }
		}
	}
}

# $TT$ localizes the papabile loss for the losing side (defender on victory, attacker on defeat)
pam_humiliation_war_church_standing_loss_effect = {
	if = {
		limit = {
			OR = {
				exists = clerical_elector_title
				exists = var:pam_papabile_score
			}
		}
		custom_tooltip = {
			text = $TT$
			if = {
				limit = { exists = var:pam_papabile_score }
				change_variable = {
					name = pam_papabile_score
					subtract = 25
				}
			}
			else = {
				set_variable = {
					name = pam_papabile_score
					value = -25
				}
			}
		}
	}
	# Only clerical rulers carry standing the disgrace can bite into; it feeds clerical appointment scoring
	if = {
		limit = { government_has_flag = government_uses_church_authority }
		add_character_modifier = {
			modifier = pam_clerical_disgrace_modifier
			years = 20
		}
	}
}

pam_on_conversion_effect = {
	if = {
		limit = { is_ai = no }
		add_character_flag = {
			flag = faith_conversion_recently_converted
			years = pam_ui_conversion_cooldown
		}
	}
}

remove_personal_tenet_if_has = {
	if = {
		limit = { has_personal_tenet = $TENET$ }
		remove_personal_tenet = $TENET$
	}
}

add_known_tenet_if_not = {
	if = {
		limit = { NOT = { knows_tenet = $TENET$ } }
		add_known_tenet = $TENET$
	}
}

update_syncretism_personal_tenet_effect = {
	if = {
		limit = {
			is_available_quick = { advanced_ruler = yes }
			scope:old_faith.religion != faith.religion
		}
		scope:old_faith = {
			if = {
				limit = { has_doctrine = sinitic_hostility_doctrine }
				prev = { add_known_tenet_if_not = { TENET = tenet_sinitic_syncretism } }
			}
			else_if = {
				limit = { has_doctrine = eastern_hostility_doctrine }
				prev = { add_known_tenet_if_not = { TENET = tenet_eastern_syncretism } }
			}
			else_if = {
				limit = { has_doctrine = pagan_hostility_doctrine }
				prev = { add_known_tenet_if_not = { TENET = tenet_unreformed_syncretism } }
			}
			else_if = {
				limit = { religion_tag = christianity_religion }
				prev = { add_known_tenet_if_not = { TENET = tenet_christian_syncretism } }
			}
			else_if = {
				limit = { religion_tag = islam_religion }
				prev = { add_known_tenet_if_not = { TENET = tenet_islamic_syncretism } }
			}
			else_if = {
				limit = { religion_tag = judaism_religion }
				prev = { add_known_tenet_if_not = { TENET = tenet_jewish_syncretism } }
			}
		}
		faith = {
			if = {
				limit = { has_doctrine = sinitic_hostility_doctrine }
				prev = { remove_personal_tenet_if_has = { TENET = tenet_sinitic_syncretism } }
			}
			else_if = {
				limit = { has_doctrine = eastern_hostility_doctrine }
				prev = { remove_personal_tenet_if_has = { TENET = tenet_eastern_syncretism } }
			}
			else_if = {
				limit = { has_doctrine = pagan_hostility_doctrine }
				prev = { remove_personal_tenet_if_has = { TENET = tenet_unreformed_syncretism } }
			}
			else_if = {
				limit = { religion_tag = christianity_religion }
				prev = { remove_personal_tenet_if_has = { TENET = tenet_christian_syncretism } }
			}
			else_if = {
				limit = { religion_tag = islam_religion }
				prev = { remove_personal_tenet_if_has = { TENET = tenet_islamic_syncretism } }
			}
			else_if = {
				limit = { religion_tag = judaism_religion }
				prev = { remove_personal_tenet_if_has = { TENET = tenet_jewish_syncretism } }
			}
		}
	}
}

# Keep branches in sync with pam_sway_to_rite_variant_can_send_trigger
pam_open_sway_to_rite_variant_effect = {
	if = {
		limit = { scope:recipient = { is_courtier_of = scope:actor } }
		open_interaction_window = {
			interaction = sway_courtier_to_my_rite_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
	}
	else_if = {
		limit = {
			scope:recipient = {
				OR = {
					target_is_liege_or_above = scope:actor
					is_tributary_of = scope:actor
					pam_rite_sway_house_lane_trigger = {
						ACTOR = scope:actor
						RECIPIENT = scope:recipient
					}
				}
			}
		}
		open_interaction_window = {
			interaction = sway_vassal_to_my_rite_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
	}
	else = {
		open_interaction_window = {
			interaction = sway_ruler_to_my_rite_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
	}
}

# Shared on_accept for the three Sway to Rite interactions
# Option flags exist only on outreach, so the ?= checks do nothing elsewhere
pam_sway_to_rite_on_accept_effect = {
	if = {
		limit = { scope:is_puppet_action ?= yes }
		scope:puppet_or_actor = {
			if = {
				limit = { pam_sway_ruler_to_my_rite_cost_value > 0 }
				add_piety = {
					value = {
						add = pam_sway_ruler_to_my_rite_cost_value
						max = piety
						multiply = -1
					}
				}
			}
		}
	}
	scope:puppet_or_actor = {
		send_interface_toast = {
			type = event_toast_effect_good
			title = sway_ruler_to_my_rite_interaction_toast
			left_icon = scope:puppet_or_actor
			right_icon = scope:recipient
			scope:recipient = {
				set_character_rite = scope:puppet_or_actor.rite
				if = {
					limit = { is_ai = yes }
					add_opinion = {
						target = scope:puppet_or_actor
						modifier = pious_opinion
						opinion = 10
					}
				}
				if = { # Accepting the founding wave of a freshly created rite pulls the whole domain and court along, but only if they are actually tied to us
					limit = {
						is_ruler = yes
						is_landed = yes
						scope:puppet_or_actor.rite ?= {
							has_variable = pam_rite_founding_window
						}
						pam_rite_sway_connected_recipient_trigger = { ACTOR = scope:puppet_or_actor }
					}
					pam_convert_domain_counties_to_rite_effect = { RITE = scope:puppet_or_actor.rite }
					pam_convert_court_to_rite_effect = { RITE = scope:puppet_or_actor.rite }
				}
				else_if = { # We also convert their capital county
					limit = {
						is_ruler = yes
						is_landed = yes
						capital_county = {
							this.faith = scope:puppet_or_actor.faith # set_county_rite would drag a foreign-faith capital across faiths
							this.rite != scope:puppet_or_actor.rite
							any_neighboring_county = { # only if a neighbor shares the same liege or IS the liege, aka I'm not surrounded by complete strangers
								OR = {
									this.holder.liege = prev.holder.liege
									this.holder = prev.holder.liege
								}
							}
						}
					}
					capital_county = { set_county_rite = scope:puppet_or_actor.rite }
				}
				if = {
					limit = { scope:pam_sway_pledge_favor ?= yes }
					add_hook = {
						target = scope:puppet_or_actor
						type = favor_hook
					}
				}
			}
			if = {
				limit = { scope:pam_sway_endowment ?= yes }
				scope:puppet_or_actor = {
					pay_treasury_or_gold = {
						target = scope:recipient
						value = pam_sway_to_rite_endowment_cost_value
					}
				}
			}
		}
		# Catalysts
		if = {
			limit = {
				scope:puppet_or_actor.religion = religion:christianity_religion
				situation:the_christian_church ?= { situation_top_has_catalyst = catalyst_the_christian_church_swayed_to_rite }
			}
			situation:the_christian_church = { trigger_situation_catalyst = catalyst_the_christian_church_swayed_to_rite }
		}
	}
	if = {
		limit = {
			scope:is_puppet_action ?= yes
			scope:actor != scope:recipient
		}
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = sway_ruler_to_my_rite_interaction_toast_puppeteer
				left_icon = scope:puppet_or_actor
				right_icon = scope:recipient
			}
		}
	}
	scope:puppet_or_actor = {
		pam_tax_nonbelievers_conversion_fulfillment_effect = yes
	}
}

# Shared on_decline for the three Sway to Rite interactions.
pam_sway_to_rite_on_decline_effect = {
	scope:puppet_or_actor = {
		send_interface_toast = {
			type = event_toast_effect_bad
			title = sway_ruler_to_my_rite_interaction_decline_toast
			right_icon = scope:recipient
			scope:recipient = {
				if = {
					limit = { is_ai = yes }
					add_opinion = {
						target = scope:puppet_or_actor
						modifier = annoyed_opinion
						opinion = -10
					}
				}
			}
		}
	}
	if = {
		limit = {
			scope:is_puppet_action ?= yes
			scope:actor != scope:recipient
		}
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_bad
				title = sway_ruler_to_my_rite_interaction_decline_toast_puppeteer
				left_icon = scope:puppet_or_actor
				right_icon = scope:recipient
			}
		}
	}
}

# PAM - Converts every county in a ruler's domain that shares their faith to $RITE$
# The capital is skipped while a different-rite clerical region holder is seated in it, unless that holder is our puppet
pam_convert_domain_counties_to_rite_effect = {
	save_temporary_scope_as = domain_rite_converter
	every_held_title = {
		limit = {
			tier = tier_county
			faith = scope:domain_rite_converter.faith
			rite != $RITE$
			trigger_if = {
				limit = { scope:domain_rite_converter.capital_county ?= this }
				NOT = {
					clerical_region_title ?= {
						holder ?= {
							capital_county = scope:domain_rite_converter.capital_county
							faith = scope:domain_rite_converter.faith
							rite != $RITE$
							NOR = {
								scope:domain_rite_converter.puppet:theological_agent_puppet ?= this
								scope:domain_rite_converter.puppet:external_theological_agent_puppet ?= this
							}
						}
					}
				}
			}
		}
		set_county_rite = $RITE$
	}
}

# PAM - Converts a ruler's court (present, away, and guests) that shares their faith to $RITE$, mirroring rite growth's court conversion
pam_convert_court_to_rite_effect = {
	every_courtier_or_guest = {
		limit = {
			is_physically_able_ai = yes
			faith = $RITE$.faith
			rite != $RITE$
			NOT = { has_character_flag = ai_will_not_convert }
		}
		set_character_rite = $RITE$
	}
	every_courtier_away = {
		limit = {
			is_physically_able_ai = yes
			faith = $RITE$.faith
			rite != $RITE$
			NOT = { has_character_flag = ai_will_not_convert }
		}
		set_character_rite = $RITE$
	}
}

# PAM - Faith counterpart of pam_convert_court_to_rite_effect: converts a ruler's court still on $OLD_FAITH$ to $FAITH$
pam_convert_court_to_faith_effect = {
	every_courtier_or_guest = {
		limit = {
			is_physically_able_ai = yes
			faith = $OLD_FAITH$
			NOT = { has_character_flag = ai_will_not_convert }
		}
		set_character_faith = $FAITH$
	}
	every_courtier_away = {
		limit = {
			is_physically_able_ai = yes
			faith = $OLD_FAITH$
			NOT = { has_character_flag = ai_will_not_convert }
		}
		set_character_faith = $FAITH$
	}
}

# PAM Great Schism: shows $TT$, and $VALUE$ goes up when positive (zero still shows the tooltip)
# Progress only goes up, and we do nothing once the schism has happened or been prevented
pam_change_great_schism_progress_effect = {
	custom_tooltip = $TT$
	hidden_effect = {
		if = {
			limit = { pam_great_schism_progress_can_change_trigger = yes }
			if = {
				limit = { $VALUE$ > 0 }
				situation:the_christian_church = {
					if = {
						limit = { NOT = { has_variable = pam_progress_towards_great_schism } }
						set_variable = { name = pam_progress_towards_great_schism value = 0 }
					}
					change_variable = {
						name = pam_progress_towards_great_schism
						add = $VALUE$
					}
				}
				every_player = {
					limit = {
						faith.religion = religion:christianity_religion
						exists = top_participant_group:the_christian_church
					}
					send_interface_message = {
						type = msg_pam_great_schism_progress
						desc = $TT$
					}
				}
			}
			if = {
				limit = {
					situation:the_christian_church = {
						NOT = { has_variable = pam_great_schism_status }
						pam_great_schism_progress_value >= pam_great_schism_progress_threshold_value
					}
				}
				situation:the_christian_church = {
					pam_update_schism_rite_variables_effect = yes
					var:schism_mainline_rite ?= { save_scope_as = schism_mainline_rite }
					var:schism_competitor_rite ?= { save_scope_as = schism_competitor_rite }
					set_variable = {
						name = pam_great_schism_status
						value = flag:threshold_reached
					}
				}
				scope:schism_mainline_rite.head_of_rite ?= { save_scope_as = schism_mainline_head }
				scope:schism_competitor_rite.head_of_rite ?= { save_scope_as = schism_competitor_head }
				every_player = {
					limit = {
						faith.religion = religion:christianity_religion
						any_character_situation = { this = situation:the_christian_church }
					}
					trigger_event = pam_the_christian_church.0064
				}
			}
		}
	}
}

# Situation scope, and a third rite as mainline keeps the existing Rome and Constantinople order
pam_update_schism_rite_variables_effect = {
	if = {
		limit = {
			#stop updating it if the schism is going to happen
			situation:the_christian_church ?= {
				NOT = { has_variable = pam_great_schism_status }
			}
		}
		if = {
			limit = { faith:christian_faith.main_rite ?= rite:roman_rite }
			set_variable = { name = schism_mainline_rite value = rite:roman_rite }
			set_variable = { name = schism_competitor_rite value = rite:byzantine_rite }
		}
		else_if = {
			limit = { faith:christian_faith.main_rite ?= rite:byzantine_rite }
			set_variable = { name = schism_mainline_rite value = rite:byzantine_rite }
			set_variable = { name = schism_competitor_rite value = rite:roman_rite }
		}
		else_if = {
			limit = { NOT = { has_variable = schism_mainline_rite } }
			set_variable = { name = schism_mainline_rite value = rite:roman_rite }
			set_variable = { name = schism_competitor_rite value = rite:byzantine_rite }
		}
	}
}

# Converters are root, with scope:old_rite the rite they left, when on_rite_change fires
# Flag spaces repeats and stops the event's own conversion from chaining
pam_schism_rite_conversion_dissent_effect = {
	if = {
		limit = {
			is_landed = yes
			exists = scope:old_rite
			scope:old_rite != rite
			OR = {
				highest_held_title_tier >= tier_kingdom
				pam_is_investiture_clergy_trigger = yes
			}
			pam_schism_rite_in_pair_trigger = { RITE = scope:old_rite }
			pam_schism_rite_in_pair_trigger = { RITE = root.rite }
			pam_great_schism_progress_can_change_trigger = yes
			NOT = { has_character_flag = pam_schism_rite_conversion_dissent_recently }
		}
		random = {
			chance = pam_schism_rite_conversion_dissent_chance_value
			scope:old_rite = { save_scope_as = pam_dissent_rite }
			add_character_flag = {
				flag = pam_schism_rite_conversion_dissent_recently
				years = 10
			}
			trigger_event = {
				id = pam_the_christian_church.0063
				days = { 3 10 }
			}
		}
	}
}

# $RITE$ is the rite they stayed with, evaluated on the character
# Shares pam_schism_rite_dissenter_valid_trigger with pam_the_christian_church.0063
pam_save_schism_rite_dissenter_effect = {
	every_courtier = {
		limit = { pam_schism_rite_dissenter_valid_trigger = { RITE = $RITE$ } }
		add_to_list = pam_schism_dissenter_candidates
	}
	every_vassal_or_below = {
		limit = { pam_schism_rite_dissenter_valid_trigger = { RITE = $RITE$ } }
		add_to_list = pam_schism_dissenter_candidates
	}
	every_pool_character = {
		province = root.location
		limit = { pam_schism_rite_dissenter_valid_trigger = { RITE = $RITE$ } }
		add_to_list = pam_schism_dissenter_candidates
	}
	random_in_list = {
		list = pam_schism_dissenter_candidates
		weight = {
			base = 1
			modifier = {
				add = 10
				pam_is_investiture_clergy_trigger = yes
			}
			modifier = {
				add = 5
				highest_held_title_tier >= tier_county
			}
			modifier = {
				add = 5
				is_close_family_of = root
			}
		}
		save_scope_as = schism_dissenter
	}
	if = {
		limit = {
			NOT = { exists = scope:schism_dissenter }
		}
		create_character = {
			template = priest_character_template
			employer = root
			faith = root.faith
			rite = $RITE$
			culture = root.culture
			save_scope_as = schism_dissenter
			after_creation = {
				add_character_flag = created
			}
		}
	}
}

# A county is root when on_county_rite_change runs this, with scope:old_rite the rite it used to have
# A faith swap books nothing, because the schism is a quarrel within Christianity
pam_schism_county_conversion_progress_effect = {
	if = {
		limit = {
			exists = scope:old_rite
			rite != scope:old_rite
			pam_schism_rite_in_pair_trigger = { RITE = scope:old_rite }
			any_county_holy_site = {
				any_holy_site_faith = { this = faith:christian_faith }
			}
		}
		if = {
			limit = { pam_schism_county_rite_in_pair_trigger = { COUNTY = root } }
			pam_change_great_schism_progress_effect = {
				VALUE = pam_great_schism_progress_county_conversion_value
				TT = pam_great_schism_progress_county_conversion_tt
			}
		}
		else = { # Attribute to the pair rite losing the county (gainer is outside the pair)
			pam_change_great_schism_progress_effect = {
				VALUE = pam_great_schism_progress_county_conversion_edge_value
				TT = pam_great_schism_progress_county_conversion_edge_tt
			}
		}
	}
}

# Fire the Great Schism catalyst again on phase change to make sure Saeculum Obscurum happens
pam_refire_great_schism_catalyst_effect = {
	if = {
		limit = {
			pam_great_schism_progress_can_change_trigger = yes
			var:pam_great_schism_status ?= flag:threshold_reached
			pam_great_schism_progress_value >= pam_great_schism_progress_threshold_value
			situation_top_has_catalyst = catalyst_the_christian_church_great_schism
		}
		trigger_situation_catalyst = catalyst_the_christian_church_great_schism
	}
}

# PAM - Great Schism: fire the schism start event for Chalcedonian rulers once conditions hold
pam_fire_great_schism_start_event_effect = {
	if = {
		limit = { var:pam_great_schism_status ?= flag:threshold_reached }
		var:schism_mainline_rite = { save_scope_as = schism_mainline_rite }
		var:schism_competitor_rite = { save_scope_as = schism_competitor_rite }
		scope:schism_mainline_rite.head_of_rite ?= { save_scope_as = schism_mainline_head }
		scope:schism_competitor_rite.head_of_rite ?= { save_scope_as = schism_competitor_head }
		set_variable = {
			name = pam_great_schism_status
			value = flag:ongoing
		}
		every_situation_participant = {
			limit = {
				is_ruler = yes
				faith = faith:christian_faith
			}
			trigger_event = pam_the_christian_church.0060
		}
	}
}

pam_offer_great_schism_stance_effect = {
	if = {
		limit = {
			pam_can_pick_great_schism_stance_trigger = yes
			NOR = {
				has_variable = pam_schism_stance
				has_character_flag = pam_offered_great_schism_stance
			}
		}
		pam_send_great_schism_stance_offer_effect = yes
	}
}

pam_offer_great_schism_stance_to_new_player_effect = {
	if = {
		limit = { pam_can_pick_great_schism_stance_trigger = yes }
		pam_send_great_schism_stance_offer_effect = yes
	}
}

pam_send_great_schism_stance_offer_effect = {
	add_character_flag = pam_offered_great_schism_stance
	situation:the_christian_church = {
		var:schism_mainline_rite = { save_scope_as = schism_mainline_rite }
		var:schism_competitor_rite = { save_scope_as = schism_competitor_rite }
	}
	trigger_event = pam_the_christian_church.0060
}

# PAM - Sacred Childbirth: scaled piety (regular tenet) for a live birth. Also triggers spiritual fulfillment and stress loss rewards (personal tenet), shown in birth events
pam_childbirth_piety_reward_effect = {
	if = {
		limit = { rite = { rite_has_parameter = piety_from_childbirth_active } }
		if = {
			limit = { scope:child = { has_any_negative_bastard_trait_trigger = no } }
			add_piety = {
				value = major_piety_gain
				if = { limit = { any_child = { is_twin_of = scope:child } } add = major_piety_gain }
				if = { limit = { any_child = { count >= 3 is_twin_of = scope:child } } add = major_piety_gain }
			}
		}
		else = {
			add_piety = {
				value = minor_piety_gain
				if = { limit = { any_child = { is_twin_of = scope:child } } add = minor_piety_gain }
				if = { limit = { any_child = { count >= 3 is_twin_of = scope:child } } add = minor_piety_gain }
			}
		}
	}
	pam_childbirth_spiritual_fulfillment_reward_effect = yes
}

# PAM - Carnal Exaltation: piety for a successful seduce scheme
pam_seduction_piety_reward_effect = {
	if = {
		limit = { rite = { rite_has_parameter = piety_from_seduction_active } }
		add_piety = medium_piety_gain
	}
}

# PAM - Sacred Childbirth: scaled spiritual fulfillment + stress loss (personal tenet) for a live birth; called on its own for children conceived in secret. shown in birth events
pam_childbirth_spiritual_fulfillment_reward_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = spiritual_fulfillment_from_childbirth_personal_active
			has_pam_dlc_trigger = yes
		}
		if = {
			limit = { scope:child = { has_any_negative_bastard_trait_trigger = no } }
			change_spiritual_fulfillment = {
				value = medium_spiritual_fulfillment_value
				if = { limit = { any_child = { is_twin_of = scope:child } } add = medium_spiritual_fulfillment_value }
				if = { limit = { any_child = { count >= 3 is_twin_of = scope:child } } add = medium_spiritual_fulfillment_value }
			}
		}
		else = {
			change_spiritual_fulfillment = {
				value = minor_spiritual_fulfillment_value
				if = { limit = { any_child = { is_twin_of = scope:child } } add = minor_spiritual_fulfillment_value }
				if = { limit = { any_child = { count >= 3 is_twin_of = scope:child } } add = minor_spiritual_fulfillment_value }
			}
		}
	}
	if = {
		limit = {
			has_personal_tenet_flag = stress_loss_from_childbirth_personal_active
			has_pam_dlc_trigger = yes
		}
		if = {
			limit = { scope:child = { has_any_negative_bastard_trait_trigger = no } }
			add_stress = {
				value = medium_stress_loss
				if = { limit = { any_child = { is_twin_of = scope:child } } add = medium_stress_loss }
				if = { limit = { any_child = { count >= 3 is_twin_of = scope:child } } add = medium_stress_loss }
			}
		}
		else = {
			add_stress = {
				value = minor_stress_loss
				if = { limit = { any_child = { is_twin_of = scope:child } } add = minor_stress_loss }
				if = { limit = { any_child = { count >= 3 is_twin_of = scope:child } } add = minor_stress_loss }
			}
		}
	}
}

# PAM - Sacred Childbirth: spiritual fulfillment loss for a child-death birth, shown in birth events
pam_childbirth_spiritual_fulfillment_penalty_effect = {
	if = {
		limit = { has_personal_tenet_flag = spiritual_fulfillment_from_childbirth_personal_active }
		if = {
			limit = { has_pam_dlc_trigger = yes }
			change_spiritual_fulfillment = major_spiritual_fulfillment_loss
		}
	}
}

# PAM - Epidemics: spiritual fulfillment loss from pestilence as divine wrath. Exaltation of Pain (faith or personal tenet) exempts the character. Needs argument AMOUNT (spiritual fulfillment script value)
pam_epidemic_spiritual_fulfillment_loss_effect = {
	if = {
		limit = {
			is_playable_character = yes
			NOR = {
				rite = { rite_has_parameter = no_epidemic_fulfillment_loss_active }
				has_personal_tenet_flag = no_epidemic_fulfillment_loss_personal_active
			}
		}
		change_spiritual_fulfillment = $AMOUNT$
	}
}

# PAM Epidemics: expects scope:epidemic, shown in epidemic arrival events
pam_epidemic_arrival_spiritual_fulfillment_loss_effect = {
	if = {
		limit = { scope:epidemic = { outbreak_intensity = apocalyptic } }
		pam_epidemic_spiritual_fulfillment_loss_effect = { AMOUNT = massive_spiritual_fulfillment_loss }
	}
	else_if = {
		limit = { scope:epidemic = { outbreak_intensity = major } }
		pam_epidemic_spiritual_fulfillment_loss_effect = { AMOUNT = major_spiritual_fulfillment_loss }
	}
	else = {
		pam_epidemic_spiritual_fulfillment_loss_effect = { AMOUNT = medium_spiritual_fulfillment_loss }
	}
}

# PAM - Epidemics: spiritual fulfillment loss when your court physician treats you for an epidemic disease (chronic diseases are exempt). Expects scope:disease_type, shown in disease treatment events
pam_epidemic_disease_treatment_spiritual_fulfillment_loss_effect = {
	if = {
		limit = {
			OR = {
				scope:disease_type = flag:bubonic_plague
				scope:disease_type = flag:smallpox
				scope:disease_type = flag:typhus
				scope:disease_type = flag:measles
				scope:disease_type = flag:dysentery
				scope:disease_type = flag:consumption
				scope:disease_type = flag:ergotism
			}
		}
		pam_epidemic_spiritual_fulfillment_loss_effect = { AMOUNT = medium_spiritual_fulfillment_loss }
	}
}

consecrate_kingdom_effect = {
	#If you don't have sway over the pope... they get sway over you
    if = {
    	limit = {
    		NOT = {
    			this = scope:religious_authority
    		}
    	}
    	if = {
    		limit = {
    			NOR = {
    				has_hook = scope:religious_authority
    				scope:king_aspirant.puppet:theological_agent_puppet ?= scope:religious_authority
    				scope:king_aspirant.puppet:external_theological_agent_puppet ?= scope:religious_authority
    			}
    		}
    		scope:religious_authority = {
    			add_hook = {
    				type = strong_obligation_hook
    				target = scope:king_aspirant
    			}
    		}
    	}
    	else = {
    		scope:religious_authority = {
    			add_hook = {
    				type = obligation_hook
    				target = scope:king_aspirant
    			}
    		}
    	}
    }
    if = {
    	limit = {
    		scope:religious_authority = {
    			this = scope:king_aspirant
    		}
    	}
    	every_held_title = {
    		limit = {
    			tier = tier_county
    		}
    		empire = {
    			every_de_jure_county = {
    				limit = {
    					holder ?= {
    						faith = scope:king_aspirant.faith
    						NOT = {
    							government_has_flag = government_is_theocracy
    						}
    					}	
    				}
    				holder = {
    					add_to_list = angry_seculars
    					every_liege_or_above = {
    						limit = {
    							faith = scope:king_aspirant.faith
    							NOT = {
    								government_has_flag = government_is_theocracy
    							}
    						}
    						add_to_list = angry_seculars
    					}
    				}
    			}
    		}
    	}
    	every_in_list = {
    		custom = custom.every_empire_secular_ruler
    		list = angry_seculars

    		add_opinion = {
    			modifier = power_hungry_priest_opinion
    			target = scope:king_aspirant
    			opinion = -30
    		}
    	}
    }
    create_custom_kingdom_effect = yes
    if = {
    	limit = {
    		NOT = {
    			this = scope:religious_authority
    		}
    		top_participant_group:the_christian_church ?= {
				OR = {
					participant_group_type = christian_clerical_main_power_rulers
					participant_group_type = christian_regular_main_power_rulers
				}
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_consecrated_kingdom_for_ruler
			}
			NOT = {
				scope:king_aspirant = { government_has_flag = government_is_theocracy }
			}
    	}
    	situation:the_christian_church = {
    		trigger_situation_catalyst = catalyst_consecrated_kingdom_for_ruler
    	}
    }
    else_if = {
    	limit = {
    		this = scope:religious_authority
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_prelate_empowered_self_with_kingdom
			}
    	}
    	situation:the_christian_church = {
    		trigger_situation_catalyst = catalyst_prelate_empowered_self_with_kingdom
    	}
    }
    else_if = {
    	limit = {
    		top_participant_group:the_christian_church ?= {
				OR = {
					participant_group_type = christian_clerical_main_power_rulers
					participant_group_type = christian_regular_main_power_rulers
				}
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_consecrated_kingdom_for_theocrat
			}
			government_has_flag = government_is_theocracy
    	}
    	situation:the_christian_church = {
    		trigger_situation_catalyst = catalyst_consecrated_kingdom_for_theocrat
    	}
    }
    if = {
        limit = {
            is_ai = no
            NOT = { exists = global_var:found_kingdom_decision  }
        }
        set_global_variable = {
            name = found_kingdom_decision
            value = root
        }
        set_global_variable = {
            name = found_kingdom_decision_kingdom
            value = root.primary_title
        }
    }
}
consecrate_kingdom_player_cleric_effect = {
    if = {
		limit = {
			is_ai = yes
		}
		if = {
			limit = {
				NOT = {
					exists = scope:interaction
				}
			}
    		add_opinion = {
				target = scope:religious_authority
				modifier = support_opinion
				opinion = 50
			}
		}
		else = {
			add_opinion = {
				target = scope:religious_authority
				modifier = support_opinion
				opinion = 25
			}
		}
		add_character_modifier = {
			modifier = consecrated_king_modifier
			desc = consecrated_king_ai_behavior_large_tt
			years = 20
		}
		custom_tooltip = consecrated_king_ai_behavior_large_tt
	}
	else = {
		add_character_modifier = {
			modifier = consecrated_king_modifier_small
			years = 20
		}
	}
	scope:religious_authority = { save_scope_as = cleric }
   	ecclesiastical_title_bestowing_clerical_hesitation_effect = yes
}
grant_title_populate_concerned_de_jure_list_effect = {
	if = {
		limit = {
			$TITLE_GAINER$ = {
				any_held_title = {
					tier = tier_duchy
					kingdom.holder ?= {
						religion = scope:puppet_or_actor.religion
						NOT = {
							this = scope:puppet_or_actor
						}
					}
				}
			}
		}
		$TITLE_GAINER$ = {
			every_held_title = {
				limit = {
					tier = tier_duchy
					kingdom.holder ?= {
						religion = scope:puppet_or_actor.religion
						NOT = {
							this = scope:puppet_or_actor
						}
					}
				}
				kingdom.holder = {
					add_to_list = grateful_refusal_chars
				}
			}
		}
	}
}
bestow_title_effect = {
	if = {
		limit = {
			scope:use_hook ?= yes
			scope:puppet_or_actor ?= { has_usable_hook = scope:recipient }
		}
		scope:puppet_or_actor = { use_hook = scope:recipient }
	}
	if = {
    	limit = {
    		scope:gain_weak_hook ?= yes
    	}
    	scope:puppet_or_actor = {
    		add_hook = {
    			type = obligation_hook
    			target = scope:recipient
    		}
    	}
    }
    else_if = {
    	limit = {
    		scope:gain_strong_hook ?= yes
    	}
    	scope:puppet_or_actor = {
    		add_hook = {
    			type = strong_obligation_hook
    			target = scope:recipient
    		}
    	}
    }
    else = {
    	if = {
    		limit = {
    			exists = scope:target
    		}
    		scope:puppet_or_actor = {
    			add_prestige = {
    				value = minor_prestige_gain
    				multiply = {
    					value = scope:target.tier
    					subtract = 2
    				}
    			}
    		}
    		
    	}
    	else = {
    		scope:puppet_or_actor = {
    			add_prestige = minor_prestige_gain
    		}
    	}
    }

	scope:recipient = { save_scope_as = bestowed_title_holder }
	scope:puppet_or_actor = { save_scope_as = bestowing_cleric }
	bestow_title_shared_effect = yes
}
# Creates the unheld title and hands it to its new holder. Runs in the new holder's scope; callers must save scope:bestowed_title_holder and scope:bestowing_cleric first, since the clergy and the ruler swap roles between the bestowal and the petition.
bestow_title_shared_effect = {
	create_title_and_vassal_change = {
		type = created
		save_scope_as = ecclesiastical_bestow_title_change
		add_claim_on_loss = no
	}
	scope:target = {
		change_title_holder = {
			holder = scope:bestowed_title_holder
			change = scope:ecclesiastical_bestow_title_change
		}
	}
	resolve_title_and_vassal_change = scope:ecclesiastical_bestow_title_change

	scope:target ?= {
		if = {
			limit = { tier = tier_empire }
			scope:bestowed_title_holder = {
				add_opinion = {
					modifier = received_title_empire
					target = scope:bestowing_cleric
					opinion = 50
				}
			}
		}
		else_if = {
			limit = { tier = tier_kingdom }
			scope:bestowed_title_holder = {
				add_opinion = {
					modifier = received_title_kingdom
					target = scope:bestowing_cleric
					opinion = 25
				}
			}
		}
		else_if = {
			limit = { tier = tier_duchy }
			scope:bestowed_title_holder = {
				add_opinion = {
					modifier = received_title_duchy
					target = scope:bestowing_cleric
					opinion = 15
				}
			}
		}
	}
    if = {
    	limit = {
    		NOT = { government_has_flag = government_is_theocracy }
    		top_participant_group:the_christian_church ?= {
				OR = {
					participant_group_type = christian_clerical_main_power_rulers
					participant_group_type = christian_regular_main_power_rulers
				}
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_hof_bestowed_title
			}
			scope:target.tier >= tier_kingdom
    	}
    	if = {
    		limit = {
    			exists = scope:target
    			scope:target.tier >= tier_empire
    		}
    		situation:the_christian_church = {
    			trigger_situation_catalyst = catalyst_hof_bestowed_title_major
    		}
    	}
    	else = {
    		situation:the_christian_church = {
    			trigger_situation_catalyst = catalyst_hof_bestowed_title
    		}
    	}
    }
    else_if = {
    	limit = {
    		government_has_flag = government_is_theocracy
    		top_participant_group:the_christian_church ?= {
				OR = {
					participant_group_type = christian_clerical_main_power_rulers
					participant_group_type = christian_regular_main_power_rulers
				}
			}
			situation:the_christian_church ?= {
				situation_top_has_catalyst = catalyst_hof_bestowed_title_clergy
			}
    	}
    	if = {
    		limit = {
    			exists = scope:target
    			scope:target.tier >= tier_empire
    		}
    		situation:the_christian_church = {
    			trigger_situation_catalyst = catalyst_hof_bestowed_title_clergy_major
    		}
    	}
    	else_if = {
    		limit = {
    			exists = scope:target
    			scope:target.tier >= tier_kingdom
    		}
    		situation:the_christian_church = {
    			trigger_situation_catalyst = catalyst_hof_bestowed_title_clergy
    		}
    	}
    	else = {
    		situation:the_christian_church = {
    			trigger_situation_catalyst = catalyst_hof_bestowed_title_clergy_minor
    		}
    	}
    }
	scope:bestowing_cleric = { save_scope_as = cleric }
    if = {
    	limit = {
    		scope:bestowing_cleric = {
    			is_ai = no
    		}
    	}
    	ecclesiastical_title_bestowing_clerical_hesitation_effect = yes
    }
    else = {
    	hidden_effect = {
    		ecclesiastical_title_bestowing_clerical_hesitation_effect = yes
    	}
    }
}
# The petition counterpart to bestow_title_effect: the clergy proclaim the unheld title and crown the petitioner. Runs in the petitioner's scope.
request_unformed_title_effect = {
	custom_tooltip = request_unformed_title_gain_tt
	if = {
		limit = {
			scope:donate_faith ?= yes
		}
		pay_short_term_gold = {
			target = scope:recipient
			gold = major_gold_value
		}
	}
	if = {
		limit = {
			scope:use_hook ?= yes
			has_usable_hook = scope:recipient
		}
		use_hook = scope:recipient
	}
	if = {
		limit = {
			scope:offer_hook ?= yes
		}
		scope:recipient = {
			add_hook = {
				type = favor_hook
				target = scope:puppet_or_actor
			}
		}
	}
	pam_offer_pilgrimage_effect = {
		ACTOR = scope:puppet_or_actor
		CLERIC = scope:recipient
	}

	scope:puppet_or_actor = { save_scope_as = bestowed_title_holder }
	scope:recipient = { save_scope_as = bestowing_cleric }
	bestow_title_shared_effect = yes

	#The clergy publicly stand behind the new crown, and their flock is expected to respect it
	if = {
		limit = {
			is_ai = yes
		}
		add_character_modifier = {
			modifier = consecrated_king_modifier
			desc = consecrated_king_ai_behavior_large_tt
			years = 20
		}
		custom_tooltip = consecrated_king_ai_behavior_large_tt
	}
	else = {
		add_character_modifier = {
			modifier = consecrated_king_modifier_small
			years = 20
		}
	}
	if = {
		limit = {
			scope:promise_holy_war ?= yes
		}
		faith = { save_scope_as = promised_holy_war_faith }
		custom_tooltip = {
			text = bonus_holy_war_tt
			add_character_flag = {
				flag = bonus_major_holy_war
				years = 5
			}
		}
		custom_tooltip = request_unformed_title_holy_war_deadline_tt
		#The clergy come looking for results; pam_interactions_events.0098 judges the vow
		trigger_event = {
			id = pam_interactions_events.0098
			years = 5
		}
	}
}

# Runs in the scope of the ruler being judged; the cleric weighing the act must be saved as scope:cleric by the caller.
ecclesiastical_title_bestowing_clerical_hesitation_effect = {
	save_scope_as = only_consideration
	if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 9
		}
		scope:cleric = {
			if = {
				limit = {
					exists = scope:target
				}
				change_spiritual_fulfillment = {
					value = major_spiritual_fulfillment_loss
					multiply = {
						value = scope:target.tier
						subtract = 2
					}
				}
			}
			else = {
				change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
			}
			custom_tooltip = clerical_hesitation_high_bestowing_tt
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 6
		}
		scope:cleric = {
			if = {
				limit = {
					exists = scope:target
				}
				change_spiritual_fulfillment = {
					value = medium_spiritual_fulfillment_loss
					multiply = {
						value = scope:target.tier
						subtract = 2
					}
				}
			}
			else = {
				change_spiritual_fulfillment = major_spiritual_fulfillment_loss
			}
			custom_tooltip = clerical_hesitation_high_bestowing_tt
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 3
		}
		scope:cleric = {
			if = {
				limit = {
					exists = scope:target
				}
				change_spiritual_fulfillment = {
					value = minor_spiritual_fulfillment_loss
					multiply = {
						value = scope:target.tier
						subtract = 2
					}
				}
			}
			else = {
				change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
			}
			custom_tooltip = clerical_hesitation_high_bestowing_tt
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= 1
		}
		scope:cleric = {
			if = {
				limit = {
					exists = scope:target
				}
				change_spiritual_fulfillment = {
					value = miniscule_spiritual_fulfillment_loss
					multiply = {
						value = scope:target.tier
						subtract = 2
					}
				}
			}
			else = {
				change_spiritual_fulfillment = minor_spiritual_fulfillment_loss
			}
			custom_tooltip = clerical_hesitation_high_bestowing_tt
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value < 1
			clerical_hesitation_claim_interaction_value >= -2
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= -3
		}
		scope:cleric = {
			if = {
				limit = {
					exists = scope:target
				}
				change_spiritual_fulfillment = {
					value = miniscule_spiritual_fulfillment_value
					divide = 2
					multiply = {
						value = scope:target.tier
						subtract = 2
					}
				}
			}
			else = {
				change_spiritual_fulfillment = miniscule_spiritual_fulfillment_value
			}
			custom_tooltip = clerical_hesitation_low_bestowing_tt
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= -5
		}
		scope:cleric = {
			if = {
				limit = {
					exists = scope:target
				}
				change_spiritual_fulfillment = {
					value = miniscule_spiritual_fulfillment_value
					multiply = {
						value = scope:target.tier
						subtract = 2
					}
				}
			}
			else = {
				change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			}
			custom_tooltip = clerical_hesitation_low_bestowing_tt
		}
	}
	else_if = {
		limit = {
			clerical_hesitation_claim_interaction_value >= -8
		}
		scope:cleric = {
			if = {
				limit = {
					exists = scope:target
				}
				change_spiritual_fulfillment = {
					value = minor_spiritual_fulfillment_value
					multiply = {
						value = scope:target.tier
						subtract = 2
					}
				}
			}
			else = {
				change_spiritual_fulfillment = medium_spiritual_fulfillment_value
			}
			custom_tooltip = clerical_hesitation_low_bestowing_tt
		}
	}
	else = {
		scope:cleric = {
			if = {
				limit = {
					exists = scope:target
				}
				change_spiritual_fulfillment = {
					value = medium_spiritual_fulfillment_value
					multiply = {
						value = scope:target.tier
						subtract = 2
					}
				}
			}
			else = {
				change_spiritual_fulfillment = major_spiritual_fulfillment_value
			}
			custom_tooltip = clerical_hesitation_low_bestowing_tt
		}
	}
}

# Never pair with a firing stress_and_fulfillment_impact on the same option
# Punishing/executing a proven witch
pam_witch_justice_fulfillment_effect = {
	if = {
		limit = { rite = { rite_has_parameter = witchcraft_illegal } }
		change_spiritual_fulfillment = medium_spiritual_fulfillment_value
	}
	else_if = {
		limit = { rite = { rite_has_parameter = witchcraft_accepted } }
		change_spiritual_fulfillment = major_spiritual_fulfillment_loss
	}
	# witchcraft_shunned: no SF either way
}

# Knowingly sparing/releasing a proven witch.
pam_witch_leniency_fulfillment_effect = {
	if = {
		limit = { rite = { rite_has_parameter = witchcraft_illegal } }
		change_spiritual_fulfillment = medium_spiritual_fulfillment_loss
	}
	else_if = {
		limit = { rite = { rite_has_parameter = witchcraft_accepted } }
		change_spiritual_fulfillment = minor_spiritual_fulfillment_value
	}
}

# Recruiting/protecting/allying with a witch.
pam_witch_embrace_fulfillment_effect = {
	if = {
		limit = { rite = { rite_has_parameter = witchcraft_illegal } }
		change_spiritual_fulfillment = major_spiritual_fulfillment_loss
	}
	else_if = {
		limit = { rite = { rite_has_doctrine = doctrine_witchcraft_virtuous } }
		change_spiritual_fulfillment = medium_spiritual_fulfillment_value
	}
	else_if = {
		limit = { rite = { rite_has_parameter = witchcraft_accepted } }
		change_spiritual_fulfillment = minor_spiritual_fulfillment_value
	}
}

add_clerical_region_with_non_christian_capital_effect = {
	$REGION_TITLE$ ?= {
		if = {
			limit = {
				holder.religion ?= religion:christianity_religion
				NOT = { title_province.religion ?= religion:christianity_religion }
			}
			scope:core_region = { add_clerical_realm_to_sub_region = prev }
		}
	}
}

ecclesiastical_de_jure_shifting_effect = {
	custom_tooltip = gain_de_jure_target_title_tt
	if = {
		limit = {
			scope:promise_domination ?= yes
		}
		if = {
			limit = {
				has_legitimacy = yes
			}
			custom_tooltip = domination_promise_tt
		}
		else = {
			custom_tooltip = domination_promise_no_legitimacy_tt
		}
		trigger_event = {
			id = pam_interactions_events.0093
			years = 5
		}
	}
	if = {
		limit = {
			scope:donate_faith ?= yes
		}
		pay_short_term_gold = {
			target = $CLERIC$
			gold = major_gold_value
		}
	}
	if = {
		limit = {
			scope:use_hook ?= yes
			has_usable_hook = $CLERIC$
		}
		use_hook = $CLERIC$
	}
	pam_offer_pilgrimage_effect = {
		ACTOR = $DE_JURE_MASTER$
		CLERIC = $CLERIC$
	}
	if = {
		limit = {
			scope:promise_conversion ?= yes
		}
		remove_treasury_or_gold = {
				value = {
					value = $DE_JURE_MASTER$.miniscule_gold_value
					if = {
						limit = {
							exists = scope:target
						}
						scope:target = {
							every_de_jure_county = {
								limit = {
									OR = {
										NOT = {
											faith = $DE_JURE_MASTER$.faith
										}
										trigger_if = {
											limit = {
												$DE_JURE_MASTER$.rite = $CLERIC$.rite
											}
											AND = {
												NOT = {
													rite = $DE_JURE_MASTER$.rite
												}
												rite = {
													"divergence(scope:de_jure_master.rite)" >= medium_rite_divergence_value
												}
											}
										}
									}
								}
								add = $DE_JURE_MASTER$.miniscule_gold_value
							}
						}
					}
					divide = 2
				}
		}
		add_piety_experience = {
				value = {
					value = $DE_JURE_MASTER$.miniscule_piety_gain
					if = {
						limit = {
							exists = scope:target
						}
						scope:target = {
							every_de_jure_county = {
								limit = {
									OR = {
										NOT = {
											faith = $DE_JURE_MASTER$.faith
										}
										trigger_if = {
											limit = {
												$DE_JURE_MASTER$.rite = $CLERIC$.rite
											}
											AND = {
												NOT = {
													rite = $DE_JURE_MASTER$.rite
												}
												rite = {
													"divergence(scope:de_jure_master.rite)" >= medium_rite_divergence_value
												}
											}
										}
									}
								}
								add = $DE_JURE_MASTER$.miniscule_piety_gain
							}
						}
					}
					divide = 2
				}
		}
		scope:target = {
				if = {
					limit = {
						$DE_JURE_MASTER$.rite = $CLERIC$.rite
						scope:target ?= {
							any_de_jure_county = {
								faith = $DE_JURE_MASTER$.faith
								NOR = {
									rite = $DE_JURE_MASTER$.rite
									holder = {
										is_ai = no
										NOR = {
											this = $DE_JURE_MASTER$
											this = $CLERIC$
										}
									}
								}
								rite = {
									"divergence(scope:de_jure_master.rite)" >= medium_rite_divergence_value
									# Freshly founded rites get a grace period before missionaries convert their counties back
									NOT = { has_variable = pam_rite_grace_period }
								}
							}
						}
					}
					every_de_jure_county = {
						custom = custom.every_non_recipient_rite_county_in_target
						limit = {
							faith = $DE_JURE_MASTER$.faith
							NOR = {
								rite = $DE_JURE_MASTER$.rite
								holder = {
									is_ai = no
									NOR = {
										this = $DE_JURE_MASTER$
										this = $CLERIC$
									}
								}
							}
							rite = {
								"divergence(scope:de_jure_master.rite)" >= medium_rite_divergence_value
								# Freshly founded rites get a grace period before missionaries convert their counties back
								NOT = { has_variable = pam_rite_grace_period }
							}
						}
						custom_tooltip = dispatch_missionaries_random_chance_of_counties_converting_rite_tt
						hidden_effect = {
							random = {
								chance = 20
								set_county_rite = $DE_JURE_MASTER$.rite
							}
						}
					}
				}
				if = {
					limit = {
						scope:target ?= {
							any_de_jure_county = {
								NOR = {
									faith = $DE_JURE_MASTER$.faith
									holder = {
										is_ai = no
										NOR = {
											this = $DE_JURE_MASTER$
											this = $CLERIC$
										}
									}
								}
							}
						}
					}
					every_de_jure_county = {
						custom = custom.every_non_actor_faith_county_in_target
						limit = {
							NOR = {
								faith = $DE_JURE_MASTER$.faith
								holder = {
									is_ai = no
									NOR = {
										this = $DE_JURE_MASTER$
										this = $CLERIC$
									}
								}
							}
						}
						custom_tooltip = dispatch_missionaries_random_chance_of_counties_converting_faith_tt
						hidden_effect = {
							random = {
								chance = 10
								set_county_faith = $DE_JURE_MASTER$.faith
							}
						}
					}
				}
		}
	}
	if = {
		limit = {
			scope:offer_hook ?= yes
		}
		$CLERIC$ = {
			add_hook = {
				type = favor_hook
				target = $DE_JURE_MASTER$
			}
		}
	}

	scope:target = {
		hidden_effect = { set_de_jure_liege_title = $DE_JURE_MASTER$.primary_title }
		if = {
			limit = {
				scope:recipient = $CLERIC$
			}
			every_de_jure_county = {
				custom = custom.every_non_actor_faith_county_controlled_in_target
				limit = {
					NOT = { faith = $DE_JURE_MASTER$.faith }
					holder = {
						target_is_same_character_or_above = $DE_JURE_MASTER$
					}
				}
				change_county_control = extreme_county_control_loss
			}
		}
		else = {
			every_de_jure_county = {
				custom = custom.every_non_recipient_faith_county_controlled_in_target
				limit = {
					NOT = { faith = $DE_JURE_MASTER$.faith }
					holder = {
						target_is_same_character_or_above = $DE_JURE_MASTER$
					}
				}
				change_county_control = extreme_county_control_loss
			}
		}
		if = {
			limit = {
				scope:recipient = $CLERIC$
			}
			every_de_jure_county = {
				custom = custom.every_non_recipient_rite_county_controlled_in_target
				limit = {
					faith = $DE_JURE_MASTER$.faith
					NOT = {
						rite = $CLERIC$.rite
					}
					holder = {
						target_is_same_character_or_above = $DE_JURE_MASTER$
					}
				}
				change_county_control = medium_county_control_loss
			}
		}
		else = {
			every_de_jure_county = {
				custom = custom.every_non_actor_rite_county_controlled_in_target
				limit = {
					faith = $DE_JURE_MASTER$.faith
					NOT = {
						rite = $CLERIC$.rite
					}
					holder = {
						target_is_same_character_or_above = $DE_JURE_MASTER$
					}
				}
				change_county_control = medium_county_control_loss
			}
		}
	}
	scope:puppet_or_actor ?= {
		if = {
			limit = {
				has_legitimacy = yes
				exists = scope:target
			}
			add_legitimacy = {
				value = medium_legitimacy_loss
				multiply = {
					value = scope:target.tier
					subtract = 2
				}
			}
		}
	}
}

pam_cardinal_vacancy_on_death_effect = {
	if = {
		limit = {
			has_pam_dlc_trigger = yes
			OR = {
				exists = clerical_elector_title
				has_character_flag = cardinal_flag
			}
			OR = {
				exists = faith.religious_head
				exists = title:k_papal_state.holder
			}
			faith = {
				has_doctrine_parameter = has_clerical_electors
				any_clerical_elector_title = { count < 10 }
			}
		}
		if = {
			limit = { exists = faith.religious_head }
			faith.religious_head = { save_scope_as = head_of_faith }
		}
		else = {
			title:k_papal_state.holder = { save_scope_as = head_of_faith }
		}
		if = {
			limit = {
				scope:head_of_faith = { is_ai = yes }
			}
			faith = {
				ordered_clerical_region_title = {
					limit = {
						holder ?= {
							is_alive = yes
							is_adult = yes
							is_incapable = no
							NOT = { has_trait = excommunicated }
							NOT = { exists = clerical_elector_title }
							this != scope:head_of_faith
							pam_can_hold_cardinalate_trigger = yes
							pam_pope_is_head_of_faith_or_rite_trigger = yes
						}
					}
					order_by = holder.piety_level
					max = 1
					check_range_bounds = no
					holder = {
						save_scope_as = cardinal_candidate
						trigger_event = {
							id = pam_clergy_events.0040
							days = 3
						}
					}
				}
			}
		}
		else = {
			faith = {
				ordered_clerical_region_title = {
					limit = {
						holder ?= {
							is_ai = yes
							is_alive = yes
							is_adult = yes
							is_incapable = no
							NOT = { has_trait = excommunicated }
							NOT = { exists = clerical_elector_title }
							this != scope:head_of_faith
							pam_can_hold_cardinalate_trigger = yes
							pam_pope_is_head_of_faith_or_rite_trigger = yes
						}
					}
					order_by = holder.piety_level
					max = 1
					check_range_bounds = no
					holder = {
						save_scope_as = cardinal_candidate
					}
				}
			}
			if = {
				limit = {
					exists = scope:cardinal_candidate
				}
				scope:head_of_faith = {
					trigger_event = {
						id = pam_clergy_events.0041
						days = 3
					}
				}
			}
		}
	}
}
demand_church_succession_side_effects = {
	primary_title = {
		every_title_heir = {
			limit = {
				NOR = {
					this = scope:actor
					this = scope:puppet_or_actor
				}
				prev = {
					place_in_line_of_succession = {
						target = prev
						value <= 3
					}
				}		
			}
			add_opinion = {
				target = scope:puppet_or_actor
				modifier = meddling_church_opinion
			}
		}
	}
}
# A Realm Priest Puppet who has grown into a titled cleric belongs in the Clerical Ruler Puppet slot,
# which also frees the Realm Priest slot for whoever takes over the council seat.
# ROOT = the puppet, $PUPPETEER$ = the ruler holding them.
# No-op if the target slot is taken, or if they still hold the court chaplain seat.
pam_make_clerical_ruler_puppet_effect = {
	# Both scopes are saved up front because clear_puppet_status severs the puppeteer link
	save_temporary_scope_as = pam_clerical_ruler_puppet
	$PUPPETEER$ = { save_temporary_scope_as = pam_clerical_puppeteer }
	if = {
		limit = {
			scope:pam_clerical_puppeteer = {
				NOR = {
					exists = puppet:external_theological_agent_puppet
					cp:councillor_court_chaplain ?= scope:pam_clerical_ruler_puppet
				}
			}
		}
		# Silent: the slot they move into is what the player is told about, not the one they leave
		hidden_effect = { clear_puppet_status = yes }
		scope:pam_clerical_puppeteer = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = make_external_clerical_puppet_on_action_notification
				left_icon = scope:pam_clerical_puppeteer
				right_icon = scope:pam_clerical_ruler_puppet
				set_puppet = {
					target = scope:pam_clerical_ruler_puppet
					type = external_theological_agent_puppet
				}
			}
		}
	}
}

pam_generic_set_puppet_interaction_side_effects = {
	if = {
		limit = { scope:hook ?= yes }
		use_hook = scope:recipient
	}
	if = {
		limit = {
			scope:skill_support ?= yes
		}
		scope:recipient = {
			if = {
				limit = {
					scope:actor = {
						highest_skill = diplomacy
					}
				}
				add_character_modifier = {
					modifier = skilled_puppeteer_modifier_diplomacy
					desc = skilled_puppeteer_behavior_tt
				}
			}
			else_if = {
				limit = {
					scope:actor = {
						highest_skill = martial
					}
				}
				add_character_modifier = {
					modifier = skilled_puppeteer_modifier_martial
					desc = skilled_puppeteer_behavior_tt
				}
			}
			else_if = {
				limit = {
					scope:actor = {
						highest_skill = stewardship
					}
				}
				add_character_modifier = {
					modifier = skilled_puppeteer_modifier_stewardship
					desc = skilled_puppeteer_behavior_tt
				}
			}
			else_if = {
				limit = {
					scope:actor = {
						highest_skill = intrigue
					}
				}
				add_character_modifier = {
					modifier = skilled_puppeteer_modifier_intrigue
					desc = skilled_puppeteer_behavior_tt
				}
			}
			else_if = {
				limit = {
					scope:actor = {
						highest_skill = learning
					}
				}
				add_character_modifier = {
					modifier = skilled_puppeteer_modifier_learning
					desc = skilled_puppeteer_behavior_tt
				}
			}
			else = {
				add_character_modifier = {
					modifier = skilled_puppeteer_modifier_general
					desc = skilled_puppeteer_behavior_tt
				}
			}
			custom_tooltip = skilled_puppeteer_behavior_tt
		}
	}
	if = {
		limit = {
			scope:piety_support ?= yes
		}
		scope:recipient = {
			if = {
				limit = { is_playable_character = yes }	# Only playable characters have spiritual fulfillment
				change_spiritual_fulfillment = medium_spiritual_fulfillment_value
			}
			if = {
				limit = {
					is_clergy = yes
				}
				add_character_modifier = {
					modifier = spiritual_puppeteer_modifier_cleric
					desc = spiritual_puppeteer_behavior_tt
				}
			}
			else = {
				add_character_modifier = {
					modifier = spiritual_puppeteer_modifier_secular
					desc = spiritual_puppeteer_behavior_tt
				}
			}
			custom_tooltip = spiritual_puppeteer_behavior_tt
		}
	}
	if = {
		limit = {
			scope:dread_support ?= yes
		}
		scope:recipient = {
			if = {
				limit = {
					OR = {
						has_trait = compassionate
						has_trait = forgiving
						has_trait = generous
						has_trait = just
					}
				}
				add_stress = medium_stress_gain
			}
			add_character_modifier = {
				modifier = dreadful_puppeteer_modifier
				desc = dreadful_puppeteer_behavior_tt
			}
			custom_tooltip = dreadful_puppeteer_behavior_tt
		}
	}
	if = {
		limit = {
			scope:friendly_support ?= yes
		}
		scope:recipient = {
			if = {
				limit = {
					scope:actor = {
						is_ai = no
					}
					OR = {
						has_trait = trusting
						has_trait = compassionate
						has_trait = generous
						has_trait = forgiving
						has_trait = just
						has_trait = humble
					}
				}
				add_trait = loyal
			}
			else = {
				random = {
					chance = {
						value = 25
						if = {
							limit = {
								OR = {
									has_trait = trusting
									has_trait = compassionate
									has_trait = generous
									has_trait = forgiving
									has_trait = just
									has_trait = humble
								}
							}
							add = 25
						}
						if = {
							limit = {
								scope:actor = {
									is_ai = no
								}
							}
							add = 25
						}
					}
					add_trait = loyal
				}
			}
			add_character_modifier = {
				modifier = beloved_puppeteer_modifier
				desc = beloved_puppeteer_behavior_tt
			}
			custom_tooltip = beloved_puppeteer_behavior_tt
		}
		stress_impact = {
			sadistic = medium_stress_impact_gain
			arbitrary = medium_stress_impact_gain
		}
	}
	if = {
		limit = {
			scope:lunacy_support ?= yes
		}
		scope:recipient = {
			if = {
				limit = {
					is_adult = yes
				}
				add_stress = massive_stress_gain
				change_spiritual_fulfillment = massive_spiritual_fulfillment_loss
			}
			else = {
				add_stress = major_stress_gain
				change_spiritual_fulfillment = major_spiritual_fulfillment_loss
			}
			add_character_modifier = {
				modifier = deranging_puppeteer_modifier
				desc = deranging_puppeteer_behavior_tt
			}
			custom_tooltip = deranging_puppeteer_behavior_tt
		}
	}
	if = {
		limit = {
			OR = {
				scope:lunacy_support ?= yes
				scope:dread_support ?= yes
			}
		}
		stress_impact = {
			compassionate = major_stress_impact_gain
			just = major_stress_impact_gain
			humble = major_stress_impact_gain
		}
	}
	if = {
		limit = {
			scope:friendly_support ?= yes
		}
		stress_impact = {
			compassionate = minor_stress_impact_gain
			trusting = minor_stress_impact_gain
			humble = minor_stress_impact_gain
			generous = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
			honest = minor_stress_impact_gain
			just = minor_stress_impact_gain
		}
	}
	if = {
		limit = {
			OR = {
				scope:skill_support ?= yes
				scope:piety_support ?= yes
				scope:dread_support ?= yes
				scope:friendly_support ?= yes
				scope:lunacy_support ?= yes
			}
		}
		scope:recipient = {
			custom_tooltip = modifier_removed_when_puppeting_ends_tt
		}
	}
	#Tracking variable that makes it easier to re-puppet former puppets
	scope:recipient = {
		add_to_variable_list = {
			name = puppeteers
			target = scope:actor
		}
	}
}
remove_puppet_modifiers_effect = {
	if = {
		limit = {
			OR = {
				has_character_modifier = skilled_puppeteer_modifier_diplomacy
				has_character_modifier = skilled_puppeteer_modifier_martial
				has_character_modifier = skilled_puppeteer_modifier_stewardship
				has_character_modifier = skilled_puppeteer_modifier_intrigue
				has_character_modifier = skilled_puppeteer_modifier_learning
				has_character_modifier = skilled_puppeteer_modifier_general
				has_character_modifier = spiritual_puppeteer_modifier_cleric
				has_character_modifier = spiritual_puppeteer_modifier_secular
				has_character_modifier = dreadful_puppeteer_modifier
				has_character_modifier = beloved_puppeteer_modifier
				has_character_modifier = deranging_puppeteer_modifier
			}
		}
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = puppet_modifier_removal.t
			if = {
				limit = {
					has_character_modifier = skilled_puppeteer_modifier_diplomacy
				}
				remove_character_modifier = skilled_puppeteer_modifier_diplomacy
			}
			if = {
				limit = {
					has_character_modifier = skilled_puppeteer_modifier_martial
				}
				remove_character_modifier = skilled_puppeteer_modifier_martial
			}
			if = {
				limit = {
					has_character_modifier = skilled_puppeteer_modifier_stewardship
				}
				remove_character_modifier = skilled_puppeteer_modifier_stewardship
			}
			if = {
				limit = {
					has_character_modifier = skilled_puppeteer_modifier_intrigue
				}
				remove_character_modifier = skilled_puppeteer_modifier_intrigue
			}
			if = {
				limit = {
					has_character_modifier = skilled_puppeteer_modifier_learning
				}
				remove_character_modifier = skilled_puppeteer_modifier_learning
			}
			if = {
				limit = {
					has_character_modifier = skilled_puppeteer_modifier_general
				}
				remove_character_modifier = skilled_puppeteer_modifier_general
			}
			if = {
				limit = {
					has_character_modifier = spiritual_puppeteer_modifier_cleric
				}
				remove_character_modifier = spiritual_puppeteer_modifier_cleric
			}
			if = {
				limit = {
					has_character_modifier = spiritual_puppeteer_modifier_secular
				}
				remove_character_modifier = spiritual_puppeteer_modifier_secular
			}
			if = {
				limit = {
					has_character_modifier = dreadful_puppeteer_modifier
				}
				remove_character_modifier = dreadful_puppeteer_modifier
			}
			if = {
				limit = {
					has_character_modifier = beloved_puppeteer_modifier
				}
				remove_character_modifier = beloved_puppeteer_modifier
			}
			if = {
				limit = {
					has_character_modifier = deranging_puppeteer_modifier
				}
				remove_character_modifier = deranging_puppeteer_modifier
			}
		}
	}
}
gnostic_tenet_learning_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = gnosticism_other_tenets
			NAND = {
				knows_tenet = tenet_astrology
				knows_tenet = tenet_inner_journey
				knows_tenet = tenet_esotericism
				knows_tenet = tenet_miaphysitism
				knows_tenet = tenet_asceticism
				knows_tenet = tenet_vows_of_poverty
				knows_tenet = tenet_pacifism
				knows_tenet = tenet_mortification_of_the_flesh
				knows_tenet = tenet_aniconism
				knows_tenet = tenet_natural_primitivism
				knows_tenet = tenet_false_conversion_sanction
				knows_tenet = tenet_consolamentum
				knows_tenet = tenet_scholasticism
				knows_tenet = tenet_pursuit_of_knowledge
				knows_tenet = tenet_alexandrian_catechism
				knows_tenet = tenet_mendicant_preachers
				knows_tenet = tenet_reincarnation
			}
		}
		random = {
			chance = {
				value = 10
				if = {
					limit = {
						has_trait_xp = {
							trait = lifestyle_scholar
							value >= 60
						}
					}
					add = 10
				}
				if = {
					limit = {
						has_trait_xp = {
							trait = lifestyle_scholar
							value >= 80
						}
					}
					add = 15
				}
				if = {
					limit = {
						has_trait_xp = {
							trait = lifestyle_scholar
							value >= 100
						}
					}
					add = 15
				}
			}
			random_list = {
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_astrology
						}
					}
					tenet:tenet_astrology = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_inner_journey
						}
					}
					tenet:tenet_inner_journey = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_esotericism
						}
					}
					tenet:tenet_esotericism = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_miaphysitism
						}
					}
					tenet:tenet_miaphysitism = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_asceticism
						}
					}
					tenet:tenet_asceticism = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_vows_of_poverty
						}
					}
					tenet:tenet_vows_of_poverty = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_pacifism
						}
					}
					tenet:tenet_pacifism = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_mortification_of_the_flesh
						}
					}
					tenet:tenet_mortification_of_the_flesh = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_aniconism
						}
					}
					tenet:tenet_aniconism = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_natural_primitivism
						}
					}
					tenet:tenet_natural_primitivism = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_false_conversion_sanction
						}
					}
					tenet:tenet_false_conversion_sanction = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_consolamentum
						}
					}
					tenet:tenet_consolamentum = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_scholasticism
						}
					}
					tenet:tenet_scholasticism = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_pursuit_of_knowledge
						}
					}
					tenet:tenet_pursuit_of_knowledge = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_alexandrian_catechism
						}
					}
					tenet:tenet_alexandrian_catechism = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_mendicant_preachers
						}
					}
					tenet:tenet_mendicant_preachers = { save_scope_as = gnostic_tenet }
				}
				1 = {
					trigger = {
						NOT = {
							knows_tenet = tenet_reincarnation
						}
					}
					tenet:tenet_reincarnation = { save_scope_as = gnostic_tenet }
				}
			}
			send_interface_message = {
				type = msg_passive_learned_tenet_special
				desc = gnostic_tenet_learning.t
				left_icon = root
				right_icon = scope:gnostic_tenet

				hidden_effect = { add_known_tenet = scope:gnostic_tenet }
				change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			}
		}
	}
}

# Character scope. Runs from yearly_playable_pulse
# The modifier stacks, so remove-all first
pam_harmonious_society_five_bonds_effect = {
	remove_all_character_modifier_instances = tenet_harmonious_society_five_bonds_modifier

	set_local_variable = {
		name = five_bonds_tally
		value = pam_five_bonds_tally_value
	}

	set_local_variable = {
		name = five_bonds_change
		value = local_var:five_bonds_tally
	}
	if = {
		limit = { has_variable = pam_five_bonds_tally_var }
		change_local_variable = {
			name = five_bonds_change
			subtract = var:pam_five_bonds_tally_var
		}
	}

	# The pulse runs every year; only say something when the tally has actually moved.
	if = {
		limit = { local_var:five_bonds_change > 0 }
		send_interface_toast = {
			type = event_toast_effect_good
			title = pam_harmonious_society_five_bonds_toast
			desc = pam_harmonious_society_five_bonds_toast_desc
			left_icon = root
			pam_harmonious_society_apply_five_bonds_effect = yes
		}
	}
	else_if = {
		limit = { local_var:five_bonds_change < 0 }
		send_interface_toast = {
			type = event_toast_effect_bad
			title = pam_harmonious_society_five_bonds_toast
			desc = pam_harmonious_society_five_bonds_toast_desc
			left_icon = root
			pam_harmonious_society_apply_five_bonds_effect = yes
		}
	}
	else = {
		hidden_effect = {
			pam_harmonious_society_apply_five_bonds_effect = yes
		}
	}

	set_variable = {
		name = pam_five_bonds_tally_var
		value = local_var:five_bonds_tally
	}
}

# Character scope. One stack for each bond kept, named individually so the toast reads as a tally.
pam_harmonious_society_apply_five_bonds_effect = {
	if = {
		limit = { pam_five_bonds_ruler_subject_trigger = yes }
		custom_tooltip = pam_five_bonds_ruler_subject_tt
		add_character_modifier = tenet_harmonious_society_five_bonds_modifier
	}
	if = {
		limit = { pam_five_bonds_parent_child_trigger = yes }
		custom_tooltip = pam_five_bonds_parent_child_tt
		add_character_modifier = tenet_harmonious_society_five_bonds_modifier
	}
	if = {
		limit = { pam_five_bonds_spouse_trigger = yes }
		custom_tooltip = pam_five_bonds_spouse_tt
		add_character_modifier = tenet_harmonious_society_five_bonds_modifier
	}
	if = {
		limit = { pam_five_bonds_sibling_trigger = yes }
		custom_tooltip = pam_five_bonds_sibling_tt
		add_character_modifier = tenet_harmonious_society_five_bonds_modifier
	}
	if = {
		limit = { pam_five_bonds_friend_trigger = yes }
		custom_tooltip = pam_five_bonds_friend_tt
		add_character_modifier = tenet_harmonious_society_five_bonds_modifier
	}
}

# One stack of $MODIFIER$ per trait the rite counts as a sin
pam_add_modifier_per_sin_trait_effect = {
	remove_all_character_modifier_instances = $MODIFIER$

	every_character_trait = {
		limit = {
			root.rite = { trait_is_sin_rite = prev }
		}
		root = {
			add_character_modifier = $MODIFIER$
		}
	}
}

pam_tenet_purgatory_sin_penalty_effect = {
	pam_add_modifier_per_sin_trait_effect = { MODIFIER = tenet_purgatory_sin_penalty_modifier }
}

pam_tenet_original_sin_insight_effect = {
	pam_add_modifier_per_sin_trait_effect = { MODIFIER = tenet_original_sin_sinners_insight_modifier }
}

# New lover or soulmate; one payout a year
pam_tenet_polyamory_new_beloved_effect = {
	if = {
		limit = {
			has_personal_tenet_flag = tenet_polyamory_new_beloved
			NOT = { has_character_flag = pam_polyamory_new_beloved_cooldown }
		}
		send_interface_toast = {
			type = event_toast_effect_good
			title = tenet_polyamory_new_beloved_toast
			left_icon = root
			change_spiritual_fulfillment = minor_spiritual_fulfillment_value
			add_stress = medium_stress_loss
			add_character_modifier = {
				modifier = tenet_polyamory_new_beloved_modifier
				years = 5
			}
		}
		add_character_flag = {
			flag = pam_polyamory_new_beloved_cooldown
			days = 365
		}
	}
}

pam_tenet_apostolic_succession_episcopate_effect = {
	remove_character_modifier = tenet_apostolic_succession_episcopate_modifier

	if = {
		limit = {
			is_valid_for_legitimacy_change = yes
			pam_apostolic_succession_bishop_count_value > 0
		}
		add_character_modifier = tenet_apostolic_succession_episcopate_modifier
	}
}

pam_tenet_divine_liturgy_choirs_effect = {
	remove_character_modifier = tenet_divine_liturgy_choirs_modifier

	if = {
		limit = {
			pam_divine_liturgy_temple_count_value > 0
		}
		add_character_modifier = tenet_divine_liturgy_choirs_modifier
	}
}

pam_tenet_divine_liturgy_officiant_effect = {
	remove_character_modifier = tenet_divine_liturgy_officiant_modifier

	if = {
		limit = {
			pam_divine_liturgy_chaplain_learning_value > 0
		}
		add_character_modifier = tenet_divine_liturgy_officiant_modifier
	}

	# Timed past yearly retally so a lapsed chaplain ages out
	if = {
		limit = {
			has_personal_tenet_flag = tenet_divine_liturgy_schola_cantorum
			cp:councillor_court_chaplain ?= { faith = root.faith }
		}
		cp:councillor_court_chaplain = {
			add_character_modifier = {
				modifier = tenet_divine_liturgy_schola_cantorum_modifier
				months = 13
			}
		}
	}
}

###############################################
# Holy Myron

# OWNER - character receiving the ampulla (artifact is created in their scope)
# CREATOR - the cleric who gathered the balsams and consecrated the oil
# RARITY - common | masterwork | famed | illustrious
pam_create_holy_myron_artifact_effect = {
	get_artifact_quality_effect = yes
	get_artifact_wealth_effect = yes

	$OWNER$ = {
		create_artifact = {
			name = pam_holy_myron_artifact_name
			description = pam_holy_myron_artifact_desc_$RARITY$
			creator = $CREATOR$
			visuals = flask
			type = miscellaneous
			template = pam_holy_myron_template
			rarity = $RARITY$
			modifier = artifact_placeholder_modifier
			decaying = no
			save_scope_as = newly_created_artifact
			wealth = scope:wealth
			quality = scope:quality
		}
	}
	scope:newly_created_artifact = {
		save_scope_as = pam_myron_vessel
		if = {
			limit = { rarity = common }
			set_variable = {
				name = pam_holy_myron_charges
				value = 1
			}
			add_artifact_modifier = pam_holy_myron_vessel_modifier
		}
		else_if = {
			limit = { rarity = masterwork }
			set_variable = {
				name = pam_holy_myron_charges
				value = 2
			}
			add_artifact_modifier = pam_holy_myron_vessel_modifier
		}
		else_if = {
			limit = { rarity = famed }
			set_variable = {
				name = pam_holy_myron_charges
				value = 3
			}
			add_artifact_modifier = pam_holy_myron_vessel_potent_modifier
		}
		else = {
			set_variable = {
				name = pam_holy_myron_charges
				value = 4
			}
			add_artifact_modifier = pam_holy_myron_vessel_potent_modifier
		}
		# We set the description again once the charge variable exists, reading it through scope:pam_myron_vessel
		set_artifact_description = pam_holy_myron_artifact_desc_charged_$RARITY$
		remove_artifact_modifier = artifact_placeholder_modifier
	}
}

# Character scope: saves the owned ampulla as scope:pam_holy_myron_artifact.
pam_save_holy_myron_artifact_effect = {
	random_character_artifact = {
		limit = { pam_is_holy_myron_artifact_trigger = yes }
		save_scope_as = pam_holy_myron_artifact
	}
}

# $ARTIFACT$ = the ampulla
# trigger_event with no delay runs after this script, so destroy (0002) never leaves a live empty vessel
pam_holy_myron_consume_charge_effect = {
	if = {
		limit = { exists = $ARTIFACT$ }
		$ARTIFACT$ = {
			change_variable = {
				name = pam_holy_myron_charges
				subtract = 1
			}
		}
		if = {
			limit = {
				$ARTIFACT$ = { var:pam_holy_myron_charges <= 0 }
			}
			$ARTIFACT$ = { save_scope_as = pam_holy_myron_spent_vessel }
			$ARTIFACT$.artifact_owner ?= {
				trigger_event = pam_holy_myron.0002
			}
		}
		else = {
			$ARTIFACT$ = {
				save_scope_as = pam_myron_vessel
				if = {
					limit = { rarity = masterwork }
					set_artifact_description = pam_holy_myron_artifact_desc_charged_masterwork
				}
				else_if = {
					limit = { rarity = famed }
					set_artifact_description = pam_holy_myron_artifact_desc_charged_famed
				}
				else_if = {
					limit = { rarity = illustrious }
					set_artifact_description = pam_holy_myron_artifact_desc_charged_illustrious
				}
				else = {
					set_artifact_description = pam_holy_myron_artifact_desc_charged_common
				}
			}
		}
	}
}

# Character scope on the owner, run alongside pam_holy_myron_consume_charge_effect at every spend site
pam_holy_myron_spend_warning_effect = {
	if = {
		limit = {
			any_character_artifact = {
				pam_is_holy_myron_artifact_trigger = yes
				trigger_if = {
					limit = { has_variable = pam_holy_myron_charges }
					var:pam_holy_myron_charges <= 1
				}
				trigger_else = {
					always = no
				}
			}
		}
		custom_tooltip = pam_holy_myron_spend_last_tt
	}
	else = {
		custom_tooltip = pam_holy_myron_spend_tt
	}
}

# $PARENT$ is the devoted parent, and the child is sealed once by whichever parent we reach first
pam_holy_myron_chrismation_effect = {
	if = {
		limit = {
			scope:child = {
				is_alive = yes
				NOT = { has_variable = pam_holy_myron_chrismated }
			}
		}
		# Twins are one ceremony, and later paths cover the sibling through scope:pam_myron_twin
		scope:child = {
			set_variable = pam_holy_myron_chrismated
			if = {
				limit = { has_trait = twin }
				random_sibling = {
					limit = {
						is_twin_of = scope:child
						is_alive = yes
						NOT = { has_variable = pam_holy_myron_chrismated }
					}
					save_scope_as = pam_myron_twin
				}
			}
		}
		scope:pam_myron_twin ?= {
			set_variable = pam_holy_myron_chrismated
		}
		if = {
			limit = {
				$PARENT$ = {
					pam_has_usable_holy_myron_trigger = yes
					is_available = yes
					exists = cp:councillor_court_chaplain
				}
			}
			$PARENT$ = {
				trigger_event = {
					id = pam_holy_myron.0300
					days = 3
				}
			}
		}
		else = {
			$PARENT$ = {
				send_interface_toast = {
					type = event_generic_good
					title = pam_holy_myron_chrismation_toast
					left_icon = scope:child
					right_icon = $PARENT$
					scope:child = {
						if = {
							limit = {
								NOR = {
									has_character_modifier = pam_chrismated_child_modifier
									has_character_modifier = pam_myron_chrismated_child_modifier
								}
							}
							add_character_modifier = {
								modifier = pam_chrismated_child_modifier
								years = 16
							}
						}
					}
					scope:pam_myron_twin ?= {
						if = {
							limit = {
								is_alive = yes
								NOR = {
									has_character_modifier = pam_chrismated_child_modifier
									has_character_modifier = pam_myron_chrismated_child_modifier
								}
							}
							add_character_modifier = {
								modifier = pam_chrismated_child_modifier
								years = 16
							}
						}
					}
				}
			}
		}
	}
}

# Chaplain scope: clears all Holy Myron journey bookkeeping (journey end, death, or abort).
pam_holy_myron_journey_cleanup_effect = {
	var:pam_holy_myron_liege ?= {
		remove_variable = pam_holy_myron_journey_active
	}
	remove_variable = pam_holy_myron_journey_tier
	remove_variable = pam_holy_myron_legs
	remove_variable = pam_holy_myron_expected_legs
	remove_variable = pam_holy_myron_robbed
	remove_variable = pam_holy_myron_ransom_fired
	remove_variable = pam_holy_myron_fever_fired
	remove_variable = pam_holy_myron_liege
	remove_character_modifier = pam_holy_myron_pilgrim_modifier
}

# Artifact scope: rarity-scaled Religious Study phase-speed modifier for devotional books
add_scaled_artifact_modifier_study_scripture_scheme_phase_duration_add_effect = {
	if = {
		limit = { rarity = common }
		add_artifact_modifier = artifact_study_scripture_scheme_phase_duration_add_1_modifier
	}
	else_if = {
		limit = { rarity = masterwork }
		add_artifact_modifier = artifact_study_scripture_scheme_phase_duration_add_2_modifier
	}
	else_if = {
		limit = { rarity = famed }
		add_artifact_modifier = artifact_study_scripture_scheme_phase_duration_add_3_modifier
	}
	else_if = {
		limit = { rarity = illustrious }
		add_artifact_modifier = artifact_study_scripture_scheme_phase_duration_add_4_modifier
	}
}

pam_evangelize_personal_tenet_effect = {
	scope:recipient = {
		pam_replace_personal_tenet_effect = { TENET = scope:pam_evangelize_tenet }
	}
	if = {
		limit = {
			scope:hook = yes
			has_usable_hook = scope:recipient
		}
		use_hook = scope:recipient
	}
	change_spiritual_fulfillment = miniscule_spiritual_fulfillment_value
	pam_tenet_popularity_on_evangelized_tenet_effect = {
		TENET = scope:pam_evangelize_tenet
		CHANGE = scope:recipient.pam_evangelize_tenet_popularity_gain
	}
	if = {
		limit = {
			faith.main_rite = {
				has_tenet_status = {
					tenet = scope:pam_evangelize_tenet
					status = prohibited
				}
			}
		}
		every_secret = {
			type = secret_forbidden_personal_tenet
			limit = { NOT = { is_known_by = scope:recipient } }
			reveal_to = scope:recipient
		}
		show_as_tooltip = {
			scope:recipient = {
				add_secret = { type = secret_forbidden_personal_tenet }
				every_secret = {
					type = secret_forbidden_personal_tenet
					reveal_to = scope:actor
				}
			}
		}
		scope:recipient = {
			set_variable = {
				name = tenet_evangelized_by
				value = scope:actor
				days = 1
			}
		}
	}
}

pam_evangelize_personal_tenet_decline_effect = {
	change_spiritual_fulfillment = miniscule_spiritual_fulfillment_loss
	if = {
		limit = {
			faith.main_rite = {
				has_tenet_status = {
					tenet = scope:pam_evangelize_tenet
					status = prohibited
				}
			}
		}
		every_secret = {
			type = secret_forbidden_personal_tenet
			limit = { NOT = { is_known_by = scope:recipient } }
			reveal_to = scope:recipient
		}
	}
}

pam_reform_order_effect = {
	pam_save_selected_interaction_tenet_effect = yes

	scope:actor.rite = { save_scope_as = pam_reform_order_rite }
	scope:recipient = {
		holy_order = {
			save_scope_as = pam_reform_order
			title = { save_scope_as = pam_reform_order_title }
			if = {
				limit = { exists = scope:pam_selected_interaction_tenet }
				custom_tooltip = { # Order adopts the tenet
					text = pam_reform_order_adopt_tenet_tt
					hidden_effect = {
						scope:pam_reform_order = { set_order_tenet = scope:pam_selected_interaction_tenet }
					}
				}
			}
			if = { # Order adopts rite
				limit = {
					NOT = { order_rite = scope:pam_reform_order_rite }
				}
				custom_tooltip = {
					text = pam_reform_order_adopt_rite_tt
					hidden_effect = {
						scope:pam_reform_order = {
							set_order_rite = scope:pam_reform_order_rite
						}
					}
				}
			}
		}
		if = { # Adopt the PT
			limit = { exists = scope:pam_selected_interaction_tenet }
			if = {
				limit = {
					NOT = { has_personal_tenet = scope:pam_selected_interaction_tenet }
				}
				pam_replace_personal_tenet_effect = { TENET = scope:pam_selected_interaction_tenet }
			}
			custom_tooltip = {
				text = pam_reform_order_courtiers_adopt_tenet_tt
				every_courtier = {
					limit = {
						OR = {
							pam_is_monastic_order_member_trigger = yes
							pam_is_military_order_member_trigger = yes
						}
						NOT = { has_personal_tenet = scope:pam_selected_interaction_tenet }
					}
					pam_replace_personal_tenet_effect = { TENET = scope:pam_selected_interaction_tenet }
				}
			}
		}
		if = { # Adopt the rite
			limit = { exists = scope:pam_reform_order_rite }
			if = {
				limit = {
					NOT = { rite = scope:pam_reform_order_rite }
				}
				set_character_rite = scope:pam_reform_order_rite
			}
			custom_tooltip = {
				text = pam_reform_order_courtiers_adopt_rite_tt
				every_courtier = {
					limit = {
						OR = {
							pam_is_monastic_order_member_trigger = yes
							pam_is_military_order_member_trigger = yes
						}
						NOT = { rite = scope:pam_reform_order_rite }
					}
					set_character_rite = scope:pam_reform_order_rite
				}
			}
		}
	}

	hidden_effect = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = pam_reform_order_interaction_toast
				right_icon = scope:recipient
				custom_tooltip = pam_reform_order_interaction_toast_tt
			}
		}
	}
}

